#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lvgl/src/font/lv_font.h"

#include <stdbool.h>
#include "lvgl/src/misc/lv_timer.h"

#include "SourceHanSansCN_Light_2_16.c"

#define USER_DATA_FILE "user_data.txt"

#define DISP_BUF_SIZE (8 * 128 * 1024)

LV_FONT_DECLARE(SourceHanSansCN_Light_2_16); // 引入字体

// 座位状态枚举
typedef enum {
    SEAT_AVAILABLE, // 可选座位（灰色）
    SEAT_SELECTED,  // 已选座位（绿色）
    SEAT_SOLD       // 已售座位（红色）
} SeatStatus;

// 座位结构体  存储座位号、状态
typedef struct
{
    uint32_t seat_num; // 座位号  1排1号
    uint8_t row;       // 排数    1~6
    uint8_t col;       // 列数    1~9
    SeatStatus status; // 座位状态
    lv_obj_t * btn;    // 座位按钮对象
} Seat;

// 取票码结构体
typedef struct
{
    char code[9];        // 8位取票码 + 结束符
    time_t create_time;  // 创建时间
    char movie_info[50]; // 电影信息(未实现)
} TicketCode;

// 存储取票码的数组
#define MAX_TICKET_CODES 100
static TicketCode ticket_codes[MAX_TICKET_CODES];
static int ticket_code_count = 0;

/**********************
 *  STATIC VARIABLES
 **********************/
static Seat seats[54];                    // 总座位  6排×9列=54个
static uint8_t selected_seats[10] = {0};  // 已选座位号 最多选10个
static uint8_t selected_count     = 0;    // 已选座位数量
static lv_obj_t * selected_label  = NULL; // 显示已选座位的标签
static void create_seat_layout(lv_obj_t * parent);
static void seat_click_cb(lv_event_t * e);
static void update_selected_label(void);
static void confirm_ticket_cb(lv_event_t * e);
static void order_btn_cb(lv_event_t * e);
static void back_to_index_timer_cb(lv_timer_t * timer);

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

// 回调函数声明
static lv_obj_t * keyboard_obj       = NULL;
static lv_obj_t * current_textarea   = NULL;
static lv_timer_t * inactivity_timer = NULL;
static lv_obj_t * username_input     = NULL;
static lv_obj_t * password_input     = NULL;
static void reset_inactivity_timer(lv_event_t * e);
static void reg_complete_cb(lv_event_t * e);                          // 完成注册按钮回调
static void back_to_login_cb(lv_event_t * e);                         // 返回登录按钮回调
static void carousel_timer_callback(lv_timer_t * timer);              // 轮播定时器回调
static void show_index_screen(void);                                  // 首页
static void create_ticket_notice_module(lv_obj_t * parent);           // 购票须知模块
static void create_self_service_ticket_purchase(lv_obj_t * parent);   // 自助购票模块
static void create_self_service_ticket_collection(lv_obj_t * parent); // 自助取票模块
static void create_collection_notice_module(lv_obj_t * parent);       // 取票须知模块
static void show_ticket_notice_screen(void);                          // 购票须知详情页
static void show_self_service_purchase_screen(void);                  // 自助购票操作页
static void show_self_service_collection_screen(void);                // 自助取票操作页
static void show_collection_notice_screen(void);                      // 取票须知详情页
static void back_to_index_cb(lv_event_t * e);                         // 返回首页通用回调
static void timer_switch_to_login_cb(lv_timer_t * timer);             // 定时器切换到登录页回调
static void inactivity_back_to_index_cb(lv_timer_t * timer);          // 无操作返回首页回调
static void my_button_event_cb(lv_event_t * e);                       // 我的按键事件回调
static void ok_btn_cb(lv_event_t * e);                                // 确认按钮回调
static void ticket_input_click_cb(lv_event_t * e);                    // 输入框点击回调
static void print_btn_cb(lv_event_t * e);                             // 打印按钮回调
static void back_btn_cb(lv_event_t * e);                              // 返回按钮回调
static void hide_keyboard_overlay_cb(lv_event_t * e);                 // 幕布点击回调
static void img_click_cb(lv_event_t * e);                             // 图片点击回调
static bool is_username_exist(const char * username);                 // 判断用户名是否存在

// 存储注册成功的账号
#define MAX_USERS 100                                   // 可以存100个用户数量
static char * registered_usernames[MAX_USERS] = {NULL}; // 用户名数组
static char * registered_passwords[MAX_USERS] = {NULL}; // 密码数组
static int registered_user_count              = 0;      // 已注册用户数量
static lv_obj_t * time_label;

// 函数声明
void show_first_img(void);
void show_second_screen(void);
void show_register_screen(void);
static void ta_event_cb(lv_event_t * e);
LV_FONT_DECLARE(SourceHanSansCN_Light_2_16);
static void hide_keyboard(void);
static void delete_obj_cb(lv_timer_t * timer);
static lv_obj_t * reg_username_input    = NULL;
static lv_obj_t * reg_password_input    = NULL;
static lv_obj_t * reg_confirm_pwd_input = NULL;
static void save_user_data(const char * username, const char * password);    // 保存用户数据到文件
static void load_user_data(void);                                            // 从文件加载所有用户数据
static void initialize_user_data(void);                                      // 初始化用户数据
static bool authenticate_user(const char * username, const char * password); // 验证用户凭证
void show_my_page(void);                                                     // 我的页面
void show_settings_main(void);                                               // 设置页面
void show_settings_screen(void);                                             // 设置页面
static lv_obj_t * settings_root = NULL;                                      // 设置页面根对象
static void return_to_home(lv_timer_t * timer);                              // 返回首页
static void cancel_btn_cb(lv_event_t * e);                                   // 取消按钮回调
static void generate_ticket_code(char * code);                               // 生成取票码
static void view_ticket_code_cb(lv_event_t * e);                             // 查看取票码
void show_ticket_code_screen(void);                                          // 显示取票码页面
static bool verify_ticket_code(const char * input_code);                     // 验证取票码
static void print_ticket_cb(lv_event_t * e);                                 // 打印票票按钮回调
static lv_obj_t * ticket_keyboard  = NULL;                                   // 键盘对象
static lv_obj_t * keyboard_overlay = NULL;                                   // 全屏透明幕布（点击外部关闭键盘）
static void hide_keyboard_overlay_cb(lv_event_t * e);                        // 幕布点击回调
static void logout_confirm_cb(lv_event_t * e);                               // 退出登录确认回调
static void logout_yes_cb(lv_event_t * e);                                   // 确认退出
static void logout_no_cb(lv_event_t * e);                                    // 取消退出
static lv_timer_t * carousel_timer = NULL;                                   // 轮播定时器
static void map_btn_cb(lv_event_t * e);                                      // 地图按钮回调

lv_obj_t ** carousel_imgs = NULL; // 存储所有图片对象的指针数组
int img_count             = 0;    // 图片总数
int current_img           = 0;    // 当前显示的图片索引
#define IMG_WIDTH 400             // 图片宽度
#define IMG_HEIGHT 240            // 图片高度
#define CAROUSEL_INTERVAL 3000    // 轮播间隔(ms)

// 保存用户数据到文件
static void save_user_data(const char * username, const char * password)
{
    FILE * file = fopen(USER_DATA_FILE, "a");
    if(file != NULL) {
        fprintf(file, "user:%s,password:%s\n", username, password);
        fclose(file);
    }
}

// 从文件加载所有用户数据
static void load_user_data(void)
{
    FILE * file = fopen(USER_DATA_FILE, "r");
    if(file == NULL) {
        return; // 文件不存在，无需加载
    }

    char line[512];
    registered_user_count = 0;

    // 逐行读取文件
    while(fgets(line, sizeof(line), file) != NULL && registered_user_count < MAX_USERS) {
        // 移除换行符
        line[strcspn(line, "\n")] = 0;

        // user:用户名,password:密码
        char * user_prefix = "user:";
        char * pass_prefix = ",password:";

        char * user_pos = strstr(line, user_prefix);
        char * pass_pos = strstr(line, pass_prefix);

        if(user_pos && pass_pos) {
            // 提取用户名
            char * username_start = user_pos + strlen(user_prefix);
            int username_len      = pass_pos - username_start;

            // 提取密码
            char * password_start = pass_pos + strlen(pass_prefix);

            // 分配内存并复制用户名
            registered_usernames[registered_user_count] = lv_mem_alloc(username_len + 1);
            if(registered_usernames[registered_user_count] != NULL) {
                strncpy(registered_usernames[registered_user_count], username_start, username_len);
                registered_usernames[registered_user_count][username_len] = '\0';

                // 分配内存并复制密码
                int password_len                            = strlen(password_start);
                registered_passwords[registered_user_count] = lv_mem_alloc(password_len + 1);
                if(registered_passwords[registered_user_count] != NULL) {
                    strcpy(registered_passwords[registered_user_count], password_start);
                    registered_user_count++;
                } else {
                    // 密码分配失败，释放用户名内存
                    lv_mem_free(registered_usernames[registered_user_count]);
                    registered_usernames[registered_user_count] = NULL;
                }
            }
        }
    }

    fclose(file);
}

// 初始化用户数据
static void initialize_user_data(void)
{
    // 初始化用户计数器
    registered_user_count = 0;

    // 尝试从文件加载所有用户数据
    load_user_data();
}

// 验证用户凭证
static bool authenticate_user(const char * username, const char * password)
{
    // 遍历所有已注册用户
    for(int i = 0; i < registered_user_count; i++) {
        if(registered_usernames[i] != NULL && registered_passwords[i] != NULL &&
           strcmp(username, registered_usernames[i]) == 0 && strcmp(password, registered_passwords[i]) == 0) {
            return true;
        }
    }
    return false;
}

// 检查用户名是否已存在
static bool is_username_exist(const char * username)
{
    for(int i = 0; i < registered_user_count; i++) {
        if(registered_usernames[i] != NULL && strcmp(username, registered_usernames[i]) == 0) {
            return true;
        }
    }
    return false;
}

// 注册按钮回调
static void register_btn_cb(lv_event_t * e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    if(event_code == LV_EVENT_CLICKED) {
        // 点击注册按钮时，清空用户名和密码输入框
        lv_textarea_set_text(username_input, "");
        lv_textarea_set_text(password_input, "");
    }
}

// 登录按钮回调
static void login_btn_cb(lv_event_t * e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    if(event_code == LV_EVENT_CLICKED) {
        hide_keyboard(); // 隐藏键盘
        // 获取输入的用户名和密码
        const char * input_username = lv_textarea_get_text(username_input);
        const char * input_password = lv_textarea_get_text(password_input);
        bool login_success          = false;

        // 验证注册用户
        if(authenticate_user(input_username, input_password)) {
            login_success = true;
        }
        // 验证默认admin账号
        else if(strcmp(input_username, "admin") == 0 && strcmp(input_password, "password") == 0) {
            login_success = true;
        }

        // 登录成功：跳转首页 + 隐藏键盘；失败：显示提示
        if(login_success) {
            hide_keyboard();     // 隐藏键盘
            if(inactivity_timer) // 登录成功后销毁无操作定时器
            {
                lv_timer_del(inactivity_timer);
                inactivity_timer = NULL;
            }
            show_index_screen(); // 跳转首页
        } else if(strlen(input_username) == 0 || strlen(input_password) == 0) {
            // 创建提示信息容器，在登录框内居中显示
            lv_obj_t * msg_container = lv_obj_create(lv_scr_act());
            lv_obj_set_size(msg_container, 300, 40);
            lv_obj_set_style_bg_opa(msg_container, LV_OPA_TRANSP, LV_PART_MAIN);
            lv_obj_set_style_border_width(msg_container, 0, LV_PART_MAIN);

            // 在登录框内居中定位
            lv_obj_align_to(msg_container, username_input, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

            // 创建提示文本
            lv_obj_t * msg = lv_label_create(msg_container);
            lv_obj_set_style_text_font(msg, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
            lv_label_set_text(msg, "用户名或密码不能为空!");
            lv_obj_center(msg);

            // 2秒后自动删除提示
            lv_timer_create(delete_obj_cb, 2000, msg_container);
            return;
        } else {
            // 登录失败提示
            // 创建提示信息容器
            lv_obj_t * msg_container = lv_obj_create(lv_scr_act());
            lv_obj_set_size(msg_container, 300, 40);
            lv_obj_set_style_bg_opa(msg_container, LV_OPA_TRANSP, LV_PART_MAIN);
            lv_obj_set_style_border_width(msg_container, 0, LV_PART_MAIN);

            // 在登录框内居中定位
            lv_obj_align_to(msg_container, username_input, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

            // 创建提示文本
            lv_obj_t * msg = lv_label_create(msg_container);
            lv_obj_set_style_text_font(msg, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
            lv_label_set_text(msg, "用户名或密码错误!");
            lv_obj_center(msg);

            // 1.5秒后自动删除提示
            lv_timer_create(delete_obj_cb, 1500, msg_container);
        }
    }
}

// 定时器回调函数：删除对象并删除定时器自身
static void delete_obj_cb(lv_timer_t * timer)
{
    lv_obj_t * obj = (lv_obj_t *)timer->user_data;
    if(obj) {
        lv_obj_del(obj);
    }
    lv_timer_del(timer);
}

static void reset_inactivity_timer(lv_event_t * e)
{
    if(inactivity_timer) {
        lv_timer_reset(inactivity_timer);
    }
}

static void first_screen_click_cb(lv_event_t * e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    if(event_code == LV_EVENT_CLICKED) {
        // 点击屏幕时切换到第二个界面
        show_second_screen();
    }
}

// 创建第一个界面
void show_first_img(void)
{
    lv_obj_t * screen = lv_scr_act();
    lv_obj_clean(screen);
    lv_scr_load(screen);
    lv_obj_add_event_cb(screen, first_screen_click_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t * img = lv_img_create(screen);
    lv_obj_set_pos(img, 0, 0);
    lv_obj_set_size(img, 800, 480);
    lv_img_set_src(img, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/bg_img.png");

    lv_obj_t * label = lv_label_create(screen);
    lv_obj_set_pos(label, 100, 190); // 位置
    // lv_obj_set_size(label, 300, 50);
    lv_obj_set_style_text_font(label, &lv_font_montserrat_24, LV_PART_MAIN);
    lv_obj_set_style_text_color(label, lv_color_hex(0x00f0f0), LV_PART_MAIN);
    lv_label_set_text(label, "Welcome to the cinema");

    lv_obj_t * label1 = lv_label_create(screen);
    lv_obj_set_pos(label1, 370, 230); // 位置
    // lv_obj_set_size(label, 300, 50);
    lv_obj_set_style_text_font(label1, &lv_font_montserrat_24, LV_PART_MAIN);
    lv_obj_set_style_text_color(label1, lv_color_hex(0x00f0f0), LV_PART_MAIN);
    lv_label_set_text(label1, "self-service ticket machine!");

    lv_obj_t * label2 = lv_label_create(screen);
    lv_obj_set_pos(label2, 310, 390); // 位置
    lv_obj_set_style_text_font(label2, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_obj_set_style_text_color(label2, lv_color_hex(0x000000), LV_PART_MAIN);
    lv_label_set_text(label2, "[点击任意位置可继续~]");
}

static void inactivity_timer_cb(lv_timer_t * timer)
{
    show_first_img(); // 返回第一屏

    // 重新设置定时器
    if(inactivity_timer) {
        inactivity_timer = NULL;
    }
}

// 无操作返回首页定时器回调
static void inactivity_back_to_index_cb(lv_timer_t * timer)
{
    show_index_screen(); // 返回首页
    lv_timer_del(timer); // 删除定时器
}

// 我的按钮事件回调函数
static void my_button_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        show_my_page(); // 跳转到我的页面
    }
}

static void second_screen_click_cb(lv_event_t * e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    if(event_code == LV_EVENT_CLICKED) {
        // 点击注册按钮时切换到注册界面
        show_register_screen();
    }
}

// 键盘
static void btnm_event_handler(lv_event_t * e)
{
    lv_obj_t * obj   = lv_event_get_target(e);
    lv_obj_t * ta    = lv_event_get_user_data(e);
    const char * txt = lv_btnmatrix_get_btn_text(obj, lv_btnmatrix_get_selected_btn(obj));

    // 增强检查，确保输入框仍然有效
    if(ta == NULL || !lv_obj_is_valid(ta)) {
        printf("输入框无效或已被删除\n");
        return;
    }

    if(strcmp(txt, LV_SYMBOL_BACKSPACE) == 0) {
        lv_textarea_del_char(ta);
    } else if(strcmp(txt, LV_SYMBOL_NEW_LINE) == 0) {
        lv_event_send(ta, LV_EVENT_READY, NULL);
    } else {
        lv_textarea_add_text(ta, txt); // 显式添加文本
    }

    lv_obj_refresh_ext_draw_size(ta); // 刷新输入框显示
    if(inactivity_timer) {
        lv_timer_reset(inactivity_timer); // 重置无操作定时器
    }
    printf("定时器已重置\n");
}

// 创建键盘并关联到指定输入框
static void create_keyboard(lv_obj_t * ta)
{
    // 如果已有键盘，先删除
    if(keyboard_obj) {
        lv_obj_del(keyboard_obj);
    }

    current_textarea = ta;

    static const char * btnm_map[] = {
        "1", "2", "3", "\n", "4", "5", "6", "\n", "7", "8", "9", "\n", LV_SYMBOL_BACKSPACE, "0", LV_SYMBOL_NEW_LINE,
        ""};

    keyboard_obj = lv_btnmatrix_create(lv_scr_act());
    lv_obj_set_size(keyboard_obj, 200, 150);
    lv_obj_align(keyboard_obj, LV_ALIGN_BOTTOM_MID, 0, -10);
    // 修复：确保将输入框作为用户数据传递给键盘
    lv_obj_add_event_cb(keyboard_obj, btnm_event_handler, LV_EVENT_VALUE_CHANGED, ta);
    lv_obj_clear_flag(keyboard_obj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
    lv_btnmatrix_set_map(keyboard_obj, btnm_map);

    // 重置无操作定时器
    if(inactivity_timer) {
        lv_timer_reset(inactivity_timer);
    }
}

// 输入框点击事件回调，用于显示键盘
static void textarea_click_cb(lv_event_t * e)
{
    lv_obj_t * ta = lv_event_get_target(e);
    create_keyboard(ta); // 为当前点击的输入框创建并显示键盘

    // 重置无操作定时器
    if(inactivity_timer) {
        lv_timer_reset(inactivity_timer);
    }
}

// 隐藏键盘的函数
static void hide_keyboard(void)
{
    if(keyboard_obj && lv_obj_is_valid(keyboard_obj)) {
        lv_obj_del(keyboard_obj);
    }
    keyboard_obj     = NULL;
    current_textarea = NULL;

    // 重置无操作定时器
    if(inactivity_timer) {
        lv_timer_reset(inactivity_timer);
    }
}

// 登录页面
void show_second_screen(void)
{
    // 重置注册页面的输入框全局变量
    reg_username_input    = NULL;
    reg_password_input    = NULL;
    reg_confirm_pwd_input = NULL;

    hide_keyboard(); // 隐藏键盘

    lv_obj_t * second_screen = lv_obj_create(NULL);
    // lv_obj_clean(second_screen);
    lv_scr_load(second_screen);
    lv_obj_set_style_bg_color(second_screen, lv_color_hex(0x00f0f0), LV_PART_MAIN);

    // 清空输入框
    if(username_input) lv_textarea_set_text(username_input, "");
    if(password_input) lv_textarea_set_text(password_input, "");

    // 无操作60秒钟返回第一界面
    if(inactivity_timer) {
        lv_timer_reset(inactivity_timer);
    } else {
        inactivity_timer = lv_timer_create(inactivity_timer_cb, 60000, NULL);
    }

    // 显示中文
    // lv_obj_t *chinese = lv_label_create(second_screen);
    // lv_obj_set_style_text_font(chinese, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    // lv_label_set_text(chinese, "欢迎来到LVGL");

    lv_obj_t * second_bg = lv_img_create(second_screen);
    lv_obj_set_pos(second_bg, 10, 20);
    lv_obj_set_size(second_bg, 320, 440);
    lv_img_set_src(second_bg, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/bg-login.png");

    // 创建登录容器
    lv_obj_t * login_box = lv_obj_create(second_screen);
    lv_obj_set_pos(login_box, 340, 20);
    lv_obj_set_size(login_box, 440, 440);
    // lv_obj_center(login_box);
    lv_obj_set_style_border_width(login_box, 2, LV_PART_MAIN);
    lv_obj_set_style_border_color(login_box, lv_color_hex(0x000000), LV_PART_MAIN);
    lv_obj_set_style_radius(login_box, 10, LV_PART_MAIN);
    lv_obj_set_style_bg_color(login_box, lv_color_hex(0xaabbcc), LV_PART_MAIN);

    // 创建登录标题
    lv_obj_t * login_title = lv_label_create(login_box);
    lv_obj_set_style_text_font(login_title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(login_title, "用户登录");
    lv_obj_set_style_text_color(login_title, lv_color_hex(0x000000), LV_PART_MAIN);
    lv_obj_align(login_title, LV_ALIGN_CENTER, 0, -190); // 顶部居中，向偏移上190

    // 用户名标签
    lv_obj_t * username_label = lv_label_create(login_box);
    lv_obj_set_style_text_font(username_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(username_label, "用户名: ");
    lv_obj_set_pos(username_label, 20, 100);

    // 用户名输入框
    username_input = lv_textarea_create(login_box);
    lv_obj_set_style_text_font(username_input, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_textarea_set_placeholder_text(username_input, "请输入用户名");
    lv_textarea_set_one_line(username_input, true);
    lv_textarea_set_max_length(username_input, 8);
    lv_obj_set_size(username_input, 280, 45);
    lv_obj_align_to(username_input, username_label, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_add_event_cb(username_input, textarea_click_cb, LV_EVENT_CLICKED, NULL);

    // 密码标签
    lv_obj_t * password_label = lv_label_create(login_box);
    lv_obj_set_style_text_font(password_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(password_label, "密    码: ");
    // lv_obj_set_style_text_font(password_label, &lv_font_montserrat_16, LV_PART_MAIN);
    lv_obj_align_to(password_label, username_label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 45);

    // 密码输入框
    password_input = lv_textarea_create(login_box);
    lv_obj_set_style_text_font(password_input, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_textarea_set_placeholder_text(password_input, "请输入密码");
    lv_textarea_set_one_line(password_input, true);
    // lv_textarea_set_password_mode(password_input, true);
    lv_textarea_set_max_length(password_input, 8);
    lv_obj_set_size(password_input, 280, 45);
    lv_obj_align_to(password_input, password_label, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_add_event_cb(password_input, textarea_click_cb, LV_EVENT_CLICKED, NULL);

    // 创建注册按钮
    lv_obj_t * register_btn = lv_btn_create(second_screen);
    lv_obj_set_pos(register_btn, 380, 300);
    lv_obj_set_size(register_btn, 150, 50);
    lv_obj_t * register_button = lv_label_create(register_btn);
    lv_obj_set_style_text_font(register_button, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(register_button, "注册");
    lv_obj_center(register_button);
    lv_obj_add_event_cb(register_btn, reset_inactivity_timer, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(register_btn, second_screen_click_cb, LV_EVENT_CLICKED, NULL);

    // 创建登录按钮
    lv_obj_t * login_btn = lv_btn_create(second_screen);
    lv_obj_set_pos(login_btn, 580, 300);
    lv_obj_set_size(login_btn, 150, 50);
    lv_obj_t * login_button = lv_label_create(login_btn);
    lv_obj_set_style_text_font(login_button, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(login_button, "登录");
    lv_obj_center(login_button);

    // 绑定登录回调函数
    lv_obj_add_event_cb(login_btn, login_btn_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(login_btn, reset_inactivity_timer, LV_EVENT_CLICKED, NULL);
}

// 完成注册按钮的回调  验证密码一致性 + 提示 + 返回登录页
static void reg_complete_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        hide_keyboard(); // 隐藏键盘
        // 从"用户数据"中取出密码输入框
        if(reg_username_input == NULL || reg_password_input == NULL || reg_confirm_pwd_input == NULL) {
            lv_obj_t * msg = lv_label_create(lv_scr_act());
            lv_obj_set_style_text_font(msg, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
            lv_label_set_text(msg, "输入框初始化失败!");
            lv_obj_center(msg);
            lv_timer_create(delete_obj_cb, 2000, msg);
            return;
        }

        // lv_obj_t *reg_username_input = input_objs[2];    // 用户名输入框
        // lv_obj_t *reg_password_input = input_objs[0];    // 密码输入框
        // lv_obj_t *reg_confirm_pwd_input = input_objs[1]; // 确认密码输入框

        const char * username    = lv_textarea_get_text(reg_username_input);
        const char * pwd         = lv_textarea_get_text(reg_password_input);
        const char * confirm_pwd = lv_textarea_get_text(reg_confirm_pwd_input);

        // 验证用户名和密码不为空
        if(strlen(username) == 0 || strlen(pwd) == 0) {
            lv_obj_t * msg = lv_label_create(lv_scr_act());
            lv_obj_set_style_text_font(msg, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
            lv_label_set_text(msg, "用户名或密码不能为空!");
            lv_obj_center(msg);
            lv_timer_create(delete_obj_cb, 2000, msg);
            return;
        }

        // 检查用户是否已存在
        if(is_username_exist(username)) {
            lv_obj_t * msg = lv_label_create(lv_scr_act());
            lv_obj_set_style_text_font(msg, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
            lv_label_set_text(msg, "该用户名已存在!");
            lv_obj_center(msg);
            lv_timer_create(delete_obj_cb, 2000, msg);
            return;
        }

        // 密码一致性验证
        if(strcmp(pwd, confirm_pwd) == 0) {
            // 检查是否达到最大用户数
            if(registered_user_count >= MAX_USERS) {
                lv_obj_t * msg = lv_label_create(lv_scr_act());
                lv_obj_set_style_text_font(msg, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
                lv_label_set_text(msg, "用户数量已达上限!");
                lv_obj_center(msg);
                lv_timer_create(delete_obj_cb, 2000, msg);
                return;
            }

            // 自动分配内存+复制字符串
            registered_usernames[registered_user_count] = lv_mem_alloc(strlen(username) + 1);
            registered_passwords[registered_user_count] = lv_mem_alloc(strlen(pwd) + 1);

            // 检查内存分配是否成功
            if(registered_usernames[registered_user_count] == NULL ||
               registered_passwords[registered_user_count] == NULL) {
                lv_obj_t * msg = lv_label_create(lv_scr_act());
                lv_obj_set_style_text_font(msg, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
                lv_label_set_text(msg, "注册失败,内存不足!");
                lv_obj_center(msg);
                lv_timer_create(delete_obj_cb, 2000, msg);

                // 清理已分配的内存
                if(registered_usernames[registered_user_count])
                    lv_mem_free(registered_usernames[registered_user_count]);
                if(registered_passwords[registered_user_count])
                    lv_mem_free(registered_passwords[registered_user_count]);
                registered_usernames[registered_user_count] = NULL;
                registered_passwords[registered_user_count] = NULL;
                return;
            }

            // 安全地复制字符串
            strcpy(registered_usernames[registered_user_count], username);
            strcpy(registered_passwords[registered_user_count], pwd);

            // 保存用户数据到文件
            save_user_data(username, pwd);

            // 增加用户计数
            registered_user_count++;

            // 提示注册成功
            lv_obj_t * msg = lv_label_create(lv_scr_act());
            lv_obj_set_style_text_font(msg, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
            lv_label_set_text(msg, "注册成功,即将返回登录页");
            lv_obj_center(msg);
            lv_timer_create(delete_obj_cb, 2000, msg); // 2秒后消失

            // 定时器切换
            lv_timer_create(timer_switch_to_login_cb, 2000, NULL);
        } else {
            lv_obj_t * msg = lv_label_create(lv_scr_act());
            lv_obj_set_style_text_font(msg, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
            lv_label_set_text(msg, "两次密码不一致");
            lv_obj_center(msg);
            lv_timer_create(delete_obj_cb, 2000, msg);
        }
    }
}

// 返回登录按钮的回调  直接切回登录页
static void back_to_login_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        hide_keyboard();

        // 重置注册页面的输入框全局变量
        reg_username_input    = NULL;
        reg_password_input    = NULL;
        reg_confirm_pwd_input = NULL;

        show_second_screen();
    }
}

// 注册页面
void show_register_screen(void)
{
    hide_keyboard(); // 隐藏键盘

    // 重置注册页面的输入框全局变量
    reg_username_input    = NULL;
    reg_password_input    = NULL;
    reg_confirm_pwd_input = NULL;

    lv_obj_t * screen = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(screen, lv_color_hex(0x000000), LV_PART_MAIN);
    // lv_obj_clean(screen);
    lv_scr_load(screen);
    lv_obj_t * reg_bg = lv_img_create(screen);
    lv_obj_set_pos(reg_bg, 0, 0);
    lv_obj_set_size(reg_bg, 800, 480);
    lv_img_set_src(reg_bg, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/re_bg.png");

    // 创建注册容器
    lv_obj_t * reg_box = lv_obj_create(screen);
    lv_obj_set_pos(reg_box, 180, 20);
    lv_obj_set_size(reg_box, 440, 440);
    lv_obj_set_style_border_width(reg_box, 2, LV_PART_MAIN);
    lv_obj_set_style_border_color(reg_box, lv_color_hex(0x0000ff), LV_PART_MAIN);
    lv_obj_set_style_radius(reg_box, 10, LV_PART_MAIN);
    lv_obj_set_style_bg_color(reg_box, lv_color_hex(0x00ffff), LV_PART_MAIN);

    // 注册标题
    lv_obj_t * reg_title = lv_label_create(reg_box);
    lv_obj_set_style_text_font(reg_title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(reg_title, "用户注册");
    lv_obj_align(reg_title, LV_ALIGN_CENTER, 0, -190);

    // 注册 用户名标签
    lv_obj_t * reg_username_label = lv_label_create(reg_box);
    lv_obj_set_style_text_font(reg_username_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(reg_username_label, "用   户  名: ");
    lv_obj_set_pos(reg_username_label, 20, 100);

    // 用户名输入框
    reg_username_input = lv_textarea_create(reg_box);
    lv_obj_set_style_text_font(reg_username_input, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_textarea_set_placeholder_text(reg_username_input, "请设置用户名");
    lv_textarea_set_one_line(reg_username_input, true);
    lv_textarea_set_max_length(reg_username_input, 8);
    lv_obj_set_size(reg_username_input, 255, 45);
    lv_obj_align_to(reg_username_input, reg_username_label, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_add_event_cb(reg_username_input, textarea_click_cb, LV_EVENT_CLICKED, NULL);

    // 注册 密码标签
    lv_obj_t * reg_password_label = lv_label_create(reg_box);
    lv_obj_set_style_text_font(reg_password_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(reg_password_label, "密        码: ");
    lv_obj_align_to(reg_password_label, reg_username_label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 45);

    // 密码输入框
    reg_password_input = lv_textarea_create(reg_box);
    lv_obj_set_style_text_font(reg_password_input, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_textarea_set_placeholder_text(reg_password_input, "请设置密码");
    lv_textarea_set_one_line(reg_password_input, true);
    // lv_textarea_set_password_mode(reg_password_input, true);
    lv_textarea_set_max_length(reg_password_input, 8);
    lv_obj_set_size(reg_password_input, 260, 45);
    lv_obj_align_to(reg_password_input, reg_password_label, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_add_event_cb(reg_password_input, textarea_click_cb, LV_EVENT_CLICKED, NULL);

    // 注册确认密码标签
    lv_obj_t * reg_confirm_pwd_label = lv_label_create(reg_box);
    lv_obj_set_style_text_font(reg_confirm_pwd_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(reg_confirm_pwd_label, "确认密码: ");
    lv_obj_align_to(reg_confirm_pwd_label, reg_password_label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 45);

    // 确认密码输入框   隐藏输入，密码模式
    reg_confirm_pwd_input = lv_textarea_create(reg_box);
    lv_obj_set_style_text_font(reg_confirm_pwd_input, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_textarea_set_placeholder_text(reg_confirm_pwd_input, "请再次输入密码");
    lv_textarea_set_one_line(reg_confirm_pwd_input, true);
    // lv_textarea_set_password_mode(reg_confirm_pwd_input, true);
    lv_textarea_set_max_length(reg_confirm_pwd_input, 8); // 最大长度限制
    lv_obj_set_size(reg_confirm_pwd_input, 260, 45);
    lv_obj_align_to(reg_confirm_pwd_input, reg_confirm_pwd_label, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_add_event_cb(reg_confirm_pwd_input, textarea_click_cb, LV_EVENT_CLICKED, NULL);

    // 完成注册按钮
    lv_obj_t * reg_btn = lv_btn_create(screen);
    lv_obj_set_pos(reg_btn, 420, 340);
    lv_obj_set_size(reg_btn, 150, 50);
    lv_obj_t * reg_btn_label = lv_label_create(reg_btn);
    lv_obj_set_style_text_font(reg_btn_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(reg_btn_label, "完成注册");
    lv_obj_center(reg_btn_label);

    // 注册完成按钮的回调
    lv_obj_add_event_cb(reg_btn, reset_inactivity_timer, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(reg_btn, reg_complete_cb, LV_EVENT_CLICKED, NULL);

    // 返回登录按钮
    lv_obj_t * back_login_btn = lv_btn_create(screen);
    lv_obj_set_pos(back_login_btn, 220, 340);
    lv_obj_set_size(back_login_btn, 150, 50);
    lv_obj_t * back_login_btn_label = lv_label_create(back_login_btn);
    lv_obj_set_style_text_font(back_login_btn_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(back_login_btn_label, "返回登录");
    lv_obj_center(back_login_btn_label);

    lv_obj_add_event_cb(back_login_btn, reset_inactivity_timer, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(back_login_btn, back_to_login_cb, LV_EVENT_CLICKED, NULL);

    // 重置无操作定时器  切换页面后，重新开始60秒计时
    if(inactivity_timer) {
        lv_timer_reset(inactivity_timer);
    } else {
        inactivity_timer = lv_timer_create(inactivity_timer_cb, 120000, NULL);
    }
}

// 图片路径数组
const char * img_paths[] = {"S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/index_bfzy.png",
                            "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/index_llsxyg.png",
                            "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/index_djg.png",
                            "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/index_njzxg.png",
                            "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/index_xgzdj.png"};

// 显示天气 静态
void create_static_display_weather(lv_obj_t * parent)
{
    // 创建天气容器
    lv_obj_t * weather_container = lv_obj_create(parent);
    lv_obj_set_pos(weather_container, 10, 420);                              // 左下角位置
    lv_obj_set_size(weather_container, 220, 50);                             // 容器大小
    lv_obj_set_style_bg_opa(weather_container, LV_OPA_TRANSP, LV_PART_MAIN); // 透明背景
    lv_obj_set_style_pad_all(weather_container, 5, LV_PART_MAIN);
    lv_obj_set_flex_flow(weather_container, LV_FLEX_FLOW_ROW); // 水平排列
    lv_obj_set_flex_align(weather_container,
                          LV_FLEX_ALIGN_START,  // 水平左对齐
                          LV_FLEX_ALIGN_CENTER, // 垂直居中
                          LV_FLEX_ALIGN_CENTER);

    // 天气文字描述
    lv_obj_t * weather_text = lv_label_create(weather_container);
    lv_obj_set_style_text_font(weather_text, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(weather_text, "今日天气: 晴");
    // lv_obj_set_style_text_font(weather_text, &lv_font_source_han_sans_cn_16, 0); // 中文支持字体
    lv_obj_set_style_text_color(weather_text, lv_color_hex(0x000000), 0); // 黑色文字

    // 天气图标
    lv_obj_t * weather_icon = lv_img_create(weather_container);
    lv_img_set_src(weather_icon,
                   "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/index_sunny.png"); // 替换为你的图标路径
    lv_obj_set_size(weather_icon, 40, 30);                                                         // 小图标尺寸
    lv_obj_set_style_pad_left(weather_icon, 0, LV_PART_MAIN);                                      // 左侧间距
    lv_obj_set_style_pad_right(weather_icon, 10, LV_PART_MAIN);                                    // 右侧间距

    // 温度显示
    lv_obj_t * temp_text = lv_label_create(weather_container);
    lv_label_set_text(temp_text, "27°C");
    lv_obj_set_pos(temp_text, 0, 0);
    lv_obj_set_style_text_font(temp_text, &lv_font_montserrat_18, 0);
    lv_obj_set_style_text_color(temp_text, lv_color_hex(0x000000), 0); // 黑色文字
}

// 显示时间
// 更新时间显示的函数
static void update_time(void)
{
    // 获取当前系统时间
    time_t now;
    time(&now);                             // 获取当前时间戳
    struct tm * timeinfo = localtime(&now); // 转换为本地时间结构

    // 格式化时间字符串  2024-09-06 15:30:45
    char time_str[20];
    strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", timeinfo);

    lv_label_set_text(time_label, time_str);
}

// 定时器中间回调
static void timer_switch_to_login_cb(lv_timer_t * timer)
{
    show_second_screen();
    lv_timer_del(timer); // 执行后删除定时器，避免重复触发
}

// 定时器回调函数（每秒触发一次）
static void time_timer_callback(lv_timer_t * timer)
{
    update_time(); // 刷新时间显示
}

// 在屏幕上创建实时时间显示
void create_real_time_display(lv_obj_t * parent)
{
    // 创建时间显示标签
    time_label = lv_label_create(parent);
    lv_obj_set_pos(time_label, 635, 455);                               // 位置
    lv_obj_set_style_text_font(time_label, &lv_font_montserrat_16, 0);  // 设置字体大小
    lv_obj_set_style_text_color(time_label, lv_color_hex(0xFFFFFF), 0); // 白色文字

    // 初始化显示当前时间
    update_time();

    // 创建定时器  1000ms = 1秒触发一次
    lv_timer_create(time_timer_callback, 1000, NULL);
}

// 购票须知模块点击回调
static void ticket_notice_click_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        show_ticket_notice_screen(); // 进入购票须知详情页
    }
}

// 购票须知模块
static void create_ticket_notice_module(lv_obj_t * parent)
{
    // 购票须知容器
    lv_obj_t * notice_ticket_box = lv_obj_create(parent);
    lv_obj_set_pos(notice_ticket_box, 0, 240);    // 位置：左0，上240
    lv_obj_set_size(notice_ticket_box, 200, 170); // 尺寸：宽200，高170
    lv_obj_set_style_pad_all(notice_ticket_box, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(notice_ticket_box, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_color(notice_ticket_box, lv_color_hex(0xE8F5E9), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(notice_ticket_box, LV_OPA_COVER, LV_PART_MAIN); // 确保背景不透明

    // 添加点击事件
    lv_obj_add_event_cb(notice_ticket_box, ticket_notice_click_cb, LV_EVENT_CLICKED, NULL);
    // 设置鼠标悬停样式
    lv_obj_set_style_bg_color(notice_ticket_box, lv_color_hex(0xC8E6C9), LV_PART_MAIN | LV_STATE_HOVERED);

    // 购票须知图标
    lv_obj_t * notice_ticket_icon = lv_img_create(notice_ticket_box);
    lv_img_set_src(notice_ticket_icon, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/notice.png");
    lv_obj_set_pos(notice_ticket_icon, 50, 32);   // 图标位置
    lv_obj_set_size(notice_ticket_icon, 100, 80); // 图标尺寸

    // 购票须知文字
    lv_obj_t * ticket_notice_label = lv_label_create(notice_ticket_box);
    lv_obj_set_style_text_font(ticket_notice_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(ticket_notice_label, "购票须知");
    lv_obj_set_pos(ticket_notice_label, 70, 120); // 文字位置
}

// 自助购票模块点击回调
static void self_service_purchase_click_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        show_self_service_purchase_screen(); // 进入自助购票操作页
    }
}

// 自助购票模块
static void create_self_service_ticket_purchase(lv_obj_t * parent)
{
    // 自助购票容器
    lv_obj_t * purchase_ticket_box = lv_obj_create(parent);
    lv_obj_set_pos(purchase_ticket_box, 200, 240);  // 位置：左200，上240
    lv_obj_set_size(purchase_ticket_box, 200, 170); // 尺寸：宽200，高170
    lv_obj_set_style_pad_all(purchase_ticket_box, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(purchase_ticket_box, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_color(purchase_ticket_box, lv_color_hex(0xFF7F50), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(purchase_ticket_box, LV_OPA_COVER, LV_PART_MAIN); // 确保背景不透明

    // 添加点击事件
    lv_obj_add_event_cb(purchase_ticket_box, self_service_purchase_click_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_bg_color(purchase_ticket_box, lv_color_hex(0xFF6347), LV_PART_MAIN | LV_STATE_HOVERED);

    // 自助购票图标
    lv_obj_t * purchase_ticket_icon = lv_img_create(purchase_ticket_box);
    lv_img_set_src(purchase_ticket_icon,
                   "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/purchase_ticket.png");
    lv_obj_set_pos(purchase_ticket_icon, 50, 32);   // 图标位置
    lv_obj_set_size(purchase_ticket_icon, 100, 80); // 图标尺寸

    // 自助购票文字
    lv_obj_t * purchase_ticket_label = lv_label_create(purchase_ticket_box);
    lv_obj_set_style_text_font(purchase_ticket_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(purchase_ticket_label, "自助购票");
    lv_obj_set_pos(purchase_ticket_label, 70, 120); // 文字位置
}

// 自助取票模块点击回调
static void self_service_collection_click_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        show_self_service_collection_screen(); // 进入自助取票操作页
    }
}

// 自助取票模块
static void create_self_service_ticket_collection(lv_obj_t * parent)
{
    // 自助取票容器
    lv_obj_t * collection_ticket_box = lv_obj_create(parent);
    lv_obj_set_pos(collection_ticket_box, 400, 240);  // 位置：左400，上240
    lv_obj_set_size(collection_ticket_box, 200, 170); // 尺寸：宽200，高170
    lv_obj_set_style_pad_all(collection_ticket_box, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(collection_ticket_box, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_color(collection_ticket_box, lv_color_hex(0x9575CD), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(collection_ticket_box, LV_OPA_COVER, LV_PART_MAIN); // 确保背景不透明

    // 添加点击事件
    lv_obj_add_event_cb(collection_ticket_box, self_service_collection_click_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_bg_color(collection_ticket_box, lv_color_hex(0x8E24AA), LV_PART_MAIN | LV_STATE_HOVERED);

    // 自助取票图标
    lv_obj_t * collection_ticket_icon = lv_img_create(collection_ticket_box);
    lv_img_set_src(collection_ticket_icon,
                   "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/collection_ticket.png");
    lv_obj_set_pos(collection_ticket_icon, 50, 32);   // 图标位置
    lv_obj_set_size(collection_ticket_icon, 100, 80); // 图标尺寸

    // 自助取票文字
    lv_obj_t * collection_ticket_label = lv_label_create(collection_ticket_box);
    lv_obj_set_style_text_font(collection_ticket_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(collection_ticket_label, "自助取票");
    lv_obj_set_pos(collection_ticket_label, 70, 120); // 文字位置
}

// 取票须知模块点击回调
static void collection_notice_click_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        show_collection_notice_screen(); // 进入取票须知详情页
    }
}

// 取票须知模块
static void create_collection_notice_module(lv_obj_t * parent)
{
    // 取票须知容器
    lv_obj_t * notice_collection_box = lv_obj_create(parent);
    lv_obj_set_pos(notice_collection_box, 600, 240);  // 位置：左600，上240
    lv_obj_set_size(notice_collection_box, 200, 170); // 尺寸：宽200，高170
    lv_obj_set_style_pad_all(notice_collection_box, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(notice_collection_box, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_color(notice_collection_box, lv_color_hex(0xE0F7FA), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(notice_collection_box, LV_OPA_COVER, LV_PART_MAIN); // 确保背景不透明

    // 添加点击事件
    lv_obj_add_event_cb(notice_collection_box, collection_notice_click_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_bg_color(notice_collection_box, lv_color_hex(0xB2EBF2), LV_PART_MAIN | LV_STATE_HOVERED);

    // 取票须知图标
    lv_obj_t * notice_collection_icon = lv_img_create(notice_collection_box);
    lv_img_set_src(notice_collection_icon, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/notice.png");
    lv_obj_set_pos(notice_collection_icon, 50, 32);   // 图标位置
    lv_obj_set_size(notice_collection_icon, 100, 80); // 图标尺寸

    // 取票须知文字
    lv_obj_t * notice_collection_label = lv_label_create(notice_collection_box);
    lv_obj_set_style_text_font(notice_collection_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(notice_collection_label, "取票须知");
    lv_obj_set_pos(notice_collection_label, 70, 120); // 文字位置
}

// 首页页面
void show_index_screen(void)
{
    // 登录成功后销毁无操作定时器
    if(inactivity_timer) {
        lv_timer_del(inactivity_timer);
        inactivity_timer = NULL;
    }

    lv_obj_t * screen = lv_obj_create(NULL);
    lv_obj_set_pos(screen, 0, 0);
    lv_obj_set_size(screen, 800, 480);
    lv_obj_set_style_bg_color(screen, lv_color_hex(0xce93d8), LV_PART_MAIN);
    lv_scr_load(screen);

    // 左上角  创建图片容器
    lv_obj_t * img_container = lv_obj_create(screen);
    lv_obj_set_pos(img_container, 0, 0);
    lv_obj_set_size(img_container, IMG_WIDTH, IMG_HEIGHT);
    lv_obj_set_style_pad_all(img_container, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(img_container, 0, LV_PART_MAIN); // 保持矩形
    lv_obj_set_style_bg_opa(img_container, LV_OPA_TRANSP, LV_PART_MAIN);
    // 设置溢出隐藏，超出容器的部分不显示
    lv_obj_set_style_clip_corner(img_container, true, LV_PART_MAIN);

    // 添加点击事件处理
    lv_obj_add_event_cb(img_container, img_click_cb, LV_EVENT_CLICKED, NULL);

    // 计算图片总数
    img_count = sizeof(img_paths) / sizeof(img_paths[0]);

    // 动态分配图片对象数组
    carousel_imgs = lv_mem_alloc(sizeof(lv_obj_t *) * img_count);
    if(carousel_imgs == NULL) return;

    // 批量创建图片对象
    for(int i = 0; i < img_count; i++) {
        carousel_imgs[i] = lv_img_create(img_container);
        lv_img_set_src(carousel_imgs[i], img_paths[i]);
        lv_obj_set_size(carousel_imgs[i], IMG_WIDTH, IMG_HEIGHT);
        // 设置初始位置：第一张在容器内，其余在右侧
        lv_obj_set_pos(carousel_imgs[i], i * IMG_WIDTH, 0);
    }

    // 创建轮播定时器
    static lv_timer_t * timer = NULL;
    if(timer) lv_timer_del(timer);
    timer = lv_timer_create(carousel_timer_callback, CAROUSEL_INTERVAL, NULL);

    /* ————————index    首页    右上角  内容———————— */
    // 右上角  创建活动公告容器
    lv_obj_t * notice_box = lv_obj_create(screen);
    lv_obj_set_pos(notice_box, 400, 0);
    lv_obj_set_size(notice_box, 400, 240);
    lv_obj_set_style_pad_all(notice_box, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(notice_box, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_opa(notice_box, LV_OPA_TRANSP, LV_PART_MAIN); // 设置透明

    // 添加背景图
    lv_obj_t * right_img = lv_img_create(screen);
    lv_obj_set_pos(right_img, 400, 0);
    lv_obj_set_size(right_img, 400, 240);
    lv_img_set_src(right_img, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/index_bg_right.png");

    // 显示所需要的文字
    lv_obj_t * notice_label = lv_label_create(notice_box);
    lv_obj_set_style_text_font(notice_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(notice_label, "今日活动公告");
    lv_obj_set_pos(notice_label, 150, 60);
    lv_obj_set_style_text_color(notice_label, lv_color_hex(0xf0f0f0), LV_PART_MAIN);
    lv_obj_move_foreground(notice_box); // 将notice_box置于最顶层  强制显示文字

    lv_obj_t * notice_label1 = lv_label_create(notice_box);
    lv_obj_set_style_text_font(notice_label1, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(notice_label1, "今日看电影免费赠送一杯可乐，请前往前台凭纸质电影票领取。");
    lv_obj_set_pos(notice_label1, 0, 150);
    lv_obj_set_style_text_color(notice_label1, lv_color_hex(0xf0f0f0), LV_PART_MAIN);
    lv_obj_move_foreground(notice_label1); // 将notice_box置于最顶层  强制显示文字

    create_ticket_notice_module(screen);           // 购票须知
    create_self_service_ticket_purchase(screen);   // 自助购票
    create_self_service_ticket_collection(screen); // 自助取票
    create_collection_notice_module(screen);       // 取票须知

    // 创建设置按钮容器
    lv_obj_t * set_button_box = lv_obj_create(screen);
    lv_obj_set_pos(set_button_box, 300, 420);
    lv_obj_set_size(set_button_box, 100, 50);
    lv_obj_set_style_pad_all(set_button_box, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_color(set_button_box, lv_color_hex(0xBBDEFB), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(set_button_box, LV_OPA_COVER, LV_PART_MAIN); // 确保背景不透明
    lv_obj_set_style_bg_color(set_button_box, lv_color_hex(0x90CAF9),
                              LV_PART_MAIN | LV_STATE_PRESSED); // 按钮点击反馈  按下时变色

    // 显示文字
    lv_obj_t * set_button_label = lv_label_create(set_button_box);
    lv_obj_set_style_text_font(set_button_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(set_button_label, "设置");
    lv_obj_set_pos(set_button_label, 30, 15);

    // 绑定点击事件：点击后进入设置页面
    lv_obj_add_event_cb(set_button_box, (lv_event_cb_t)show_settings_screen, LV_EVENT_CLICKED, NULL);

    // 为按钮添加“鼠标悬停”样式
    lv_obj_set_style_bg_color(set_button_box, lv_color_hex(0xC9E7FF), LV_PART_MAIN | LV_STATE_HOVERED);

    // 创建我的按钮容器
    lv_obj_t * my_button_box = lv_obj_create(screen);
    lv_obj_set_pos(my_button_box, 400, 420);
    lv_obj_set_size(my_button_box, 100, 50);
    lv_obj_set_style_pad_all(my_button_box, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_color(my_button_box, lv_color_hex(0xE0E0E0), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(my_button_box, LV_OPA_COVER, LV_PART_MAIN); // 确保背景不透明

    // 显示文字
    lv_obj_t * my_button_label = lv_label_create(my_button_box);
    lv_obj_set_style_text_font(my_button_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(my_button_label, "我的");
    lv_obj_set_pos(my_button_label, 30, 15);

    // 添加点击事件处理
    lv_obj_add_event_cb(my_button_box, my_button_event_cb, LV_EVENT_CLICKED, NULL);

    // 时间
    create_real_time_display(screen);

    // 天气
    create_static_display_weather(screen);
}
// 添加图片点击回调函数
static void img_click_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        // 点击时切换到下一张图片
        carousel_timer_callback(carousel_timer);

        // 重置定时器，避免点击后立即自动切换
        if(carousel_timer) {
            lv_timer_reset(carousel_timer);
        }
    }
}

// 轮播定时器回调函  切换任意数量图片
static void carousel_timer_callback(lv_timer_t * timer)
{
    // 计算下一张图片索引  循环切换
    int next_img = (current_img + 1) % img_count;

    // 移动所有图片：整体向左移动一个图片宽度
    for(int i = 0; i < img_count; i++) {
        lv_coord_t x = lv_obj_get_x(carousel_imgs[i]) - IMG_WIDTH;
        // 当图片移动到最左侧不可见区域时，循环到右侧等待
        if(x <= -IMG_WIDTH * img_count) {
            x += IMG_WIDTH * img_count;
        }
        lv_obj_set_pos(carousel_imgs[i], x, 0);
    }

    // 更新当前图片索引
    current_img = next_img;
}

// 返回首页通用回调
static void back_to_index_cb(lv_event_t * e)
{
    show_index_screen();
}

// 购票须知详情页
static void show_ticket_notice_screen(void)
{
    lv_obj_t * screen = lv_obj_create(NULL);
    lv_obj_set_size(screen, 800, 480);
    lv_obj_set_style_bg_color(screen, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    lv_scr_load(screen);

    // 标题
    lv_obj_t * title = lv_label_create(screen);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(title, "购票须知");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);

    // 内容
    lv_obj_t * content = lv_label_create(screen);
    lv_obj_set_style_text_font(content, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(content, "尊敬的观众您好,为确保您在自助取票机完成购票时流程顺畅和权益清晰,敬请阅读以下规则：\n"
                               "1. 购票需提供有效身份证件\n"
                               "2. 每张身份证限购10张票\n"
                               "3. 退票需在电影开播前半小时办理\n"
                               "4. 儿童票需由成人陪同购买\n"
                               "5. 学生票需出示有效学生证\n"
                               "6. 有问题请联系前台工作人员");
    lv_obj_set_pos(content, 50, 80);
    lv_obj_set_width(content, 700); // 限制宽度自动换行

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(screen);
    lv_obj_set_pos(back_btn, 350, 400);
    lv_obj_set_size(back_btn, 100, 50);
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_obj_set_style_text_font(back_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
    lv_obj_add_event_cb(back_btn, back_to_index_cb, LV_EVENT_CLICKED, NULL);
}

// 自助购票操作页
static void show_self_service_purchase_screen(void)
{
    lv_obj_t * screen = lv_obj_create(NULL);
    lv_obj_set_size(screen, 800, 480);
    lv_obj_set_style_bg_color(screen, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    lv_scr_load(screen);
    selected_count = 0; // 重置已选座位数量

    // 标题
    lv_obj_t * title = lv_label_create(screen);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(title, "自助购票");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);

    // 电影选择 下拉框
    lv_obj_t * movie_label = lv_label_create(screen);
    lv_obj_set_style_text_font(movie_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(movie_label, "选择电影: ");
    lv_obj_set_pos(movie_label, 50, 60);

    lv_obj_t * movie_dropdown         = lv_dropdown_create(screen);
    static const char * movie_options = "《流浪地球3》\n《疯狂动物城2》\n《哪吒2》\n《志愿军2》\n《捕风捉影》\n"
                                        "《东极岛》\n《流浪地球》\n《年会不能停》\n《无名之辈》 ";
    lv_dropdown_set_options(movie_dropdown, movie_options);

    // 设置下拉框按钮区域字体
    lv_obj_set_style_text_font(movie_dropdown, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);

    // 为下拉列表区域设置字体（显示所有选项）
    lv_obj_t * movie_list = lv_dropdown_get_list(movie_dropdown);
    lv_obj_set_style_text_font(movie_list, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);

    // 设置下拉框整体大小
    lv_obj_set_size(movie_dropdown, 200, 40);

    // 使用图标作为下拉按钮
    lv_dropdown_set_symbol(movie_dropdown, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/pull_down.png");
    lv_obj_set_style_bg_img_opa(movie_dropdown, LV_OPA_COVER, LV_PART_INDICATOR);
    lv_obj_set_style_width(movie_dropdown, 24, LV_PART_INDICATOR);    // 图标宽度
    lv_obj_set_style_height(movie_dropdown, 24, LV_PART_INDICATOR);   // 高度
    lv_obj_set_style_pad_right(movie_dropdown, 5, LV_PART_INDICATOR); // 右边距

    lv_obj_align_to(movie_dropdown, movie_label, LV_ALIGN_OUT_RIGHT_MID, 20, 0);

    // 场次选择  下拉框
    lv_obj_t * time_label = lv_label_create(screen);
    lv_obj_set_style_text_font(time_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(time_label, "选择场次: ");
    lv_obj_set_pos(time_label, 400, 60);

    static const char * time_options = "10:30\n13:15\n16:40\n19:20\n22:00";
    lv_obj_t * time_dropdown         = lv_dropdown_create(screen);
    lv_dropdown_set_options(time_dropdown, time_options);

    // 设置下拉框整体大小
    lv_obj_set_size(time_dropdown, 200, 40);

    // 使用图标作为下拉按钮
    lv_dropdown_set_symbol(time_dropdown, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/pull_down.png");
    lv_obj_set_style_bg_img_opa(time_dropdown, LV_OPA_COVER, LV_PART_INDICATOR);
    lv_obj_set_style_width(time_dropdown, 24, LV_PART_INDICATOR);    // 图标宽度
    lv_obj_set_style_height(time_dropdown, 24, LV_PART_INDICATOR);   // 图标高度
    lv_obj_set_style_pad_right(time_dropdown, 5, LV_PART_INDICATOR); // 右边距

    lv_obj_align_to(time_dropdown, time_label, LV_ALIGN_OUT_RIGHT_MID, 20, 0);

    // 嵌入座位布局
    create_seat_layout(screen);

    // 已选座位显示
    selected_label = lv_label_create(screen);
    lv_obj_set_style_text_font(selected_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(selected_label, "已选座位: 无");
    lv_obj_set_style_text_color(selected_label, lv_color_hex(0xFF6600), LV_PART_MAIN); // 橙色高亮
    lv_obj_align(selected_label, LV_ALIGN_BOTTOM_LEFT, 50, -75);

    // 确认购票按钮
    lv_obj_t * confirm_btn = lv_btn_create(screen);
    lv_obj_set_pos(confirm_btn, 320, 410);
    lv_obj_set_size(confirm_btn, 160, 50);
    lv_obj_t * confirm_btn_label = lv_label_create(confirm_btn);
    lv_obj_set_style_text_font(confirm_btn_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(confirm_btn_label, "确认购票(35元/张)");
    lv_obj_center(confirm_btn_label);

    // 确认购票回调
    lv_obj_add_event_cb(confirm_btn, confirm_ticket_cb, LV_EVENT_CLICKED, NULL);

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(screen);
    lv_obj_set_pos(back_btn, 50, 410);
    lv_obj_set_size(back_btn, 100, 50);
    lv_obj_t * back_btn_label = lv_label_create(back_btn);
    lv_obj_set_style_text_font(back_btn_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(back_btn_label, "返回首页");
    lv_obj_center(back_btn_label);
    lv_obj_add_event_cb(back_btn, back_to_index_cb, LV_EVENT_CLICKED, NULL);
}

// 确认购票回调函数
static void confirm_ticket_cb(lv_event_t * e)
{
    // 生成订单信息
    char order_msg[256] = "已选座位: ";
    int total_price     = selected_count * 35;
    for(int i = 0; i < selected_count; i++) {
        if(i > 0) strcat(order_msg, ",");
        char num_str[4];
        sprintf(num_str, "%d", selected_seats[i]);
        strcat(order_msg, num_str);
    }
    sprintf(order_msg + strlen(order_msg), "\n总价: %d元\n是否确认支付?", total_price);

    // 创建消息框容器
    lv_obj_t * msg_box = lv_obj_create(lv_scr_act());
    lv_obj_set_size(msg_box, 400, 200);
    lv_obj_center(msg_box);
    lv_obj_set_style_radius(msg_box, 10, LV_PART_MAIN);                       // 圆角优化
    lv_obj_set_style_bg_color(msg_box, lv_color_hex(0xFFFFFF), LV_PART_MAIN); // 白色背景
    lv_obj_set_style_border_width(msg_box, 2, LV_PART_MAIN);
    lv_obj_set_style_border_color(msg_box, lv_color_hex(0x000000), LV_PART_MAIN);

    // 消息框标题
    lv_obj_t * title = lv_label_create(msg_box);
    lv_label_set_text(title, "购票确认");
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_obj_set_style_text_align(title, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 15); // 顶部居中，下偏移15

    // 消息框内容
    lv_obj_t * content = lv_label_create(msg_box);
    lv_label_set_text(content, order_msg);
    lv_obj_set_style_text_font(content, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_obj_set_style_text_align(content, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
    lv_obj_align(content, LV_ALIGN_CENTER, 0, 0); // 垂直居中

    // 创建确认支付按钮
    lv_obj_t * confirm_btn = lv_btn_create(msg_box);
    lv_obj_set_size(confirm_btn, 120, 40);                                        // 按钮尺寸
    lv_obj_align(confirm_btn, LV_ALIGN_BOTTOM_LEFT, 40, 0);                       // 左下角，内边距40
    lv_obj_set_style_bg_color(confirm_btn, lv_color_hex(0x4CAF50), LV_PART_MAIN); // 绿色按钮

    // 确认支付按钮标签
    lv_obj_t * confirm_label = lv_label_create(confirm_btn);
    lv_label_set_text(confirm_label, "确认支付");
    lv_obj_set_style_text_font(confirm_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_obj_set_style_text_color(confirm_label, lv_color_hex(0xFFFFFF), LV_PART_MAIN); // 白色文字
    lv_obj_center(confirm_label);

    // 创建取消按钮
    lv_obj_t * cancel_btn = lv_btn_create(msg_box);
    lv_obj_set_size(cancel_btn, 120, 40);                                        // 按钮尺寸
    lv_obj_align(cancel_btn, LV_ALIGN_BOTTOM_RIGHT, -40, 0);                     // 右下角，内边距40
    lv_obj_set_style_bg_color(cancel_btn, lv_color_hex(0xFF4444), LV_PART_MAIN); // 红色按钮

    // 取消按钮标签
    lv_obj_t * cancel_label = lv_label_create(cancel_btn);
    lv_label_set_text(cancel_label, "取消");
    lv_obj_set_style_text_font(cancel_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_obj_set_style_text_color(cancel_label, lv_color_hex(0xFFFFFF), LV_PART_MAIN); // 白色文字
    lv_obj_center(cancel_label);

    // 绑定按钮点击事件   确认支付：触发支付成功提示   传递总价作为用户数据
    lv_obj_add_event_cb(confirm_btn, order_btn_cb, LV_EVENT_CLICKED, (void *)(intptr_t)total_price);
    // 取消：直接关闭消息框
    lv_obj_add_event_cb(cancel_btn, cancel_btn_cb, LV_EVENT_CLICKED, msg_box);
}

// 取消按钮回调函数
static void cancel_btn_cb(lv_event_t * e)
{
    // 获取要删除的消息框对象
    lv_obj_t * msg_box = lv_event_get_user_data(e);
    // 确认对象有效后删除
    if(msg_box) {
        lv_obj_del(msg_box);
    }
}

// 订单按钮回调  添加未选座位判断
static void order_btn_cb(lv_event_t * e)
{
    lv_obj_t * msg_box        = lv_event_get_target(e);              // 获取触发事件的按钮
    lv_obj_t * parent_msg_box = lv_obj_get_parent(msg_box);          // 获取消息框容器
    int total_price           = (intptr_t)lv_event_get_user_data(e); // 获取总价

    // 关闭原确认弹窗
    lv_obj_del(parent_msg_box);

    // 判断是否选择了座位   总价为0表示未选择
    if(total_price == 0) {
        // 显示未选座位提示
        lv_obj_t * error_msg = lv_obj_create(lv_scr_act());
        lv_obj_set_size(error_msg, 350, 150);
        lv_obj_center(error_msg);
        lv_obj_set_style_radius(error_msg, 10, LV_PART_MAIN);
        lv_obj_set_style_bg_color(error_msg, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
        lv_obj_set_style_border_width(error_msg, 2, LV_PART_MAIN);

        // 提示标题
        lv_obj_t * error_title = lv_label_create(error_msg);
        lv_label_set_text(error_title, "提示");
        lv_obj_set_style_text_font(error_title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
        lv_obj_align(error_title, LV_ALIGN_TOP_MID, 0, 15);

        // 提示内容
        lv_obj_t * error_content = lv_label_create(error_msg);
        lv_label_set_text(error_content, "您还未选择座位，请先选择座位");
        lv_obj_set_style_text_font(error_content, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
        lv_obj_align(error_content, LV_ALIGN_CENTER, 0, 0);

        // 添加 确定 按钮
        lv_obj_t * ok_btn = lv_btn_create(error_msg);
        lv_obj_set_size(ok_btn, 80, 35);
        lv_obj_align(ok_btn, LV_ALIGN_BOTTOM_MID, 0, 0);
        lv_obj_set_style_bg_color(ok_btn, lv_color_hex(0x4CAF50), LV_PART_MAIN);

        lv_obj_t * ok_label = lv_label_create(ok_btn);
        lv_label_set_text(ok_label, "确定");
        lv_obj_set_style_text_font(ok_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
        lv_obj_set_style_text_color(ok_label, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
        lv_obj_center(ok_label);

        // 绑定自定义回调
        lv_obj_add_event_cb(ok_btn, ok_btn_cb, LV_EVENT_CLICKED, error_msg);
    } else {
        // 正常支付流程：生成并保存取票码
        char ticket_code[9];
        generate_ticket_code(ticket_code); // 生成8位取票码

        // 保存取票码到数组
        if(ticket_code_count < MAX_TICKET_CODES) {
            strcpy(ticket_codes[ticket_code_count].code, ticket_code);
            time(&ticket_codes[ticket_code_count].create_time);
            // 虚拟
            strcpy(ticket_codes[ticket_code_count].movie_info, "复仇者联盟4");
            ticket_code_count++;
        }

        // 显示支付成功提示  取票码指引
        lv_obj_t * success_msg = lv_obj_create(lv_scr_act());
        lv_obj_set_size(success_msg, 350, 180);
        lv_obj_center(success_msg);
        lv_obj_set_style_radius(success_msg, 10, LV_PART_MAIN);
        lv_obj_set_style_bg_color(success_msg, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
        lv_obj_set_style_border_width(success_msg, 2, LV_PART_MAIN);

        lv_obj_t * success_title = lv_label_create(success_msg);
        lv_label_set_text(success_title, "提示");
        lv_obj_set_style_text_font(success_title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
        lv_obj_align(success_title, LV_ALIGN_TOP_MID, 0, 15);

        lv_obj_t * success_content = lv_label_create(success_msg);
        lv_label_set_text(success_content, "支付成功！即将返回首页\n您的取票码已生成\n点击<我的>查看取票码");
        lv_obj_set_style_text_font(success_content, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
        lv_obj_align(success_content, LV_ALIGN_CENTER, 0, 0);

        lv_timer_t * timer = lv_timer_create(return_to_home, 2500, success_msg);
        lv_timer_set_repeat_count(timer, 1);
    }
}

static void ok_btn_cb(lv_event_t * e)
{
    lv_obj_t * error_msg = lv_event_get_user_data(e); // 获取要删除的提示弹窗
    if(error_msg) {
        lv_obj_del(error_msg); // 关闭提示弹窗，回到购票页面
    }
}

// 返回首页
static void return_to_home(lv_timer_t * timer)
{
    lv_obj_t * success_msg = timer->user_data;
    lv_obj_del(success_msg);
    show_index_screen();
}

/**
 * 支付成功后返回首页的定时器回调
 */
static void back_to_index_timer_cb(lv_timer_t * timer)
{
    show_index_screen();
    lv_timer_del(timer);
}

// 验证取票码是否存在（返回true=存在，false=不存在）
static bool verify_ticket_code(const char * input_code)
{
    if(input_code == NULL || strlen(input_code) == 0) {
        return false; // 空码（保持原有“未输入”逻辑）
    }
    for(int i = 0; i < ticket_code_count; i++) {
        if(strcmp(ticket_codes[i].code, input_code) == 0) {
            return true; // 找到匹配的取票码
        }
    }
    return false; // 未找到匹配码
}

// 自助取票操作页
void show_self_service_collection_screen(void)
{
    // 进入页面时隐藏残留键盘
    hide_keyboard();

    if(keyboard_overlay) lv_obj_del(keyboard_overlay);
    keyboard_overlay = NULL;

    lv_obj_t * screen = lv_obj_create(NULL);
    lv_obj_set_size(screen, 800, 480);
    lv_scr_load(screen);
    lv_obj_set_style_bg_color(screen, lv_color_hex(0xFFFFFF), LV_PART_MAIN);

    // 标题
    lv_obj_t * title = lv_label_create(screen);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(title, "自助取票");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);

    // 取票码标签
    lv_obj_t * ticket_label = lv_label_create(screen);
    lv_obj_set_style_text_font(ticket_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(ticket_label, "取票码:");
    lv_obj_set_pos(ticket_label, 100, 150);

    // 取票码输入框
    lv_obj_t * ticket_input = lv_textarea_create(screen);
    lv_obj_set_style_text_font(ticket_input, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_obj_set_size(ticket_input, 250, 50);
    lv_obj_align_to(ticket_input, ticket_label, LV_ALIGN_OUT_RIGHT_MID, 20, 0);

    // 绑定输入框点击事件  对象、回调、事件类型、用户数据
    lv_obj_add_event_cb(ticket_input,          // obj：输入框
                        ticket_input_click_cb, // event_cb：输入框点击回调
                        LV_EVENT_CLICKED,      // filter：仅响应点击事件
                        NULL                   // user_data：无额外数据
    );

    // 打印纸质票按钮
    lv_obj_t * print_btn = lv_btn_create(screen);
    lv_obj_set_pos(print_btn, 250, 250);
    lv_obj_set_size(print_btn, 150, 50);
    lv_obj_t * print_label = lv_label_create(print_btn);
    lv_obj_set_style_text_font(print_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(print_label, "打印纸质票");
    lv_obj_center(print_label);
    lv_obj_add_event_cb(print_btn,        // obj：打印按钮
                        print_btn_cb,     // event_cb：打印按钮回调
                        LV_EVENT_CLICKED, // filter：仅响应点击事件
                        ticket_input      // user_data：传递输入框对象给回调
    );

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(screen);
    lv_obj_set_pos(back_btn, 450, 250);
    lv_obj_set_size(back_btn, 100, 50);
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_obj_set_style_text_font(back_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
    lv_obj_add_event_cb(back_btn,         // obj：返回按钮
                        back_btn_cb,      // event_cb：返回按钮回调
                        LV_EVENT_CLICKED, // filter：仅响应点击事件
                        NULL              // user_data：无额外数据
    );
}

// 输入框点击：显示键盘+创建透明幕布
static void ticket_input_click_cb(lv_event_t * e)
{
    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {
        lv_obj_t * ticket_input = lv_event_get_target(e); // 获取输入框

        // 1. 创建/显示数字键盘
        create_keyboard(ticket_input);

        // 2. 创建透明幕布 点击外部关闭键盘
        if(!keyboard_overlay) {
            keyboard_overlay = lv_obj_create(lv_scr_act());
            lv_obj_set_size(keyboard_overlay, LV_HOR_RES, LV_VER_RES);  // 全屏
            lv_obj_set_style_bg_opa(keyboard_overlay, 0, LV_PART_MAIN); // 透明
            lv_obj_add_flag(keyboard_overlay, LV_OBJ_FLAG_CLICKABLE);   // 允许点击
            // 隐藏键盘
            lv_obj_add_event_cb(keyboard_overlay, hide_keyboard_overlay_cb, LV_EVENT_CLICKED, NULL);
        }
        // 确保键盘显示在最前面
        if(keyboard_obj) {
            lv_obj_move_foreground(keyboard_obj);
        }
    }
}

// 隐藏键盘+销毁幕布
static void hide_keyboard_overlay_cb(lv_event_t * e)
{
    hide_keyboard(); // 隐藏键盘

    if(keyboard_overlay) {
        lv_obj_del(keyboard_overlay); // 销毁幕布
        keyboard_overlay = NULL;
    }
}

// 打印纸质票按钮点击：关闭键盘+销毁幕布
static void print_btn_cb(lv_event_t * e)
{
    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {

        hide_keyboard();

        if(keyboard_overlay) {
            lv_obj_del(keyboard_overlay);
            keyboard_overlay = NULL;
        }

        // 传递输入框对象
        lv_obj_t * ticket_input = (lv_obj_t *)lv_event_get_user_data(e);
        print_ticket_cb(e);
    }
}

// 返回按钮点击：关闭键盘+销毁幕布
static void back_btn_cb(lv_event_t * e)
{
    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {

        hide_keyboard();

        if(keyboard_overlay) {
            lv_obj_del(keyboard_overlay);
            keyboard_overlay = NULL;
        }

        back_to_index_cb(e);
    }
}

// 成功提示的定时器回调：删除容器 + 销毁自身
static void success_timer_cb(lv_timer_t * timer)
{
    lv_obj_t * cont = (lv_obj_t *)timer->user_data;
    if(cont != NULL) {
        lv_obj_del(cont); // 删除提示容器
    }
    lv_timer_del(timer); // 销毁定时器自身（避免内存泄漏）
}

// 错误提示的定时器回调：删除容器 + 销毁自身
static void error_timer_cb(lv_timer_t * timer)
{
    lv_obj_t * cont = (lv_obj_t *)timer->user_data;
    if(cont != NULL) {
        lv_obj_del(cont); // 删除提示容器
    }
    lv_timer_del(timer); // 销毁定时器自身
}

// 打印纸质票按钮回调
static void print_ticket_cb(lv_event_t * e)
{
    // 获取取票码输入框
    lv_obj_t * ticket_input = (lv_obj_t *)lv_event_get_user_data(e);
    const char * input_code = lv_textarea_get_text(ticket_input);

    if(input_code == NULL || strlen(input_code) == 0) {
        return; // 未输入取票码，保持原逻辑
    }

    if(verify_ticket_code(input_code)) {
        // ---------- 打印成功：自定义无标题提示 ----------
        lv_obj_t * success_cont = lv_obj_create(lv_scr_act());
        lv_obj_set_size(success_cont, 130, 70); // 提示框尺寸
        lv_obj_center(success_cont);            // 屏幕居中
        // 样式：白色背景 + 灰色边框 + 圆角
        lv_obj_set_style_radius(success_cont, 10, LV_PART_MAIN);
        lv_obj_set_style_bg_color(success_cont, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
        lv_obj_set_style_border_width(success_cont, 2, LV_PART_MAIN);
        lv_obj_set_style_border_color(success_cont, lv_color_hex(0xCCCCCC), LV_PART_MAIN);

        // 成功文字标签
        lv_obj_t * success_lbl = lv_label_create(success_cont);
        lv_label_set_text(success_lbl, "打印成功!");
        lv_obj_set_style_text_font(success_lbl, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
        lv_obj_set_style_text_align(success_lbl, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
        lv_obj_set_width(success_lbl, LV_PCT(100));       // 文字占满容器宽度
        lv_obj_align(success_lbl, LV_ALIGN_CENTER, 0, 0); // 文字居中

        // 2.5秒后自动消失
        lv_timer_create(success_timer_cb, 2500, success_cont);
    } else {
        // ---------- 取票码错误：自定义无标题提示 ----------
        lv_obj_t * error_cont = lv_obj_create(lv_scr_act());
        lv_obj_set_size(error_cont, 350, 70); // 错误提示框尺寸
        lv_obj_center(error_cont);
        // 样式与成功提示一致
        lv_obj_set_style_radius(error_cont, 10, LV_PART_MAIN);
        lv_obj_set_style_bg_color(error_cont, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
        lv_obj_set_style_border_width(error_cont, 2, LV_PART_MAIN);
        lv_obj_set_style_border_color(error_cont, lv_color_hex(0xCCCCCC), LV_PART_MAIN);

        // 错误文字标签
        lv_obj_t * error_lbl = lv_label_create(error_cont);
        lv_label_set_text(error_lbl, "该取票码有误或无该取票码, 请重新输入~");
        lv_obj_set_style_text_font(error_lbl, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
        lv_obj_set_style_text_align(error_lbl, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
        lv_obj_set_width(error_lbl, LV_PCT(100));
        lv_obj_align(error_lbl, LV_ALIGN_CENTER, 0, 0);

        // 2.5秒后自动消失
        lv_timer_create(error_timer_cb, 2500, error_cont);
    }
}

// 取票须知详情页
static void show_collection_notice_screen(void)
{
    lv_obj_t * screen = lv_obj_create(NULL);
    lv_obj_set_size(screen, 800, 480);
    lv_obj_set_style_bg_color(screen, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    lv_scr_load(screen);

    // 标题
    lv_obj_t * title = lv_label_create(screen);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(title, "取票须知");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);

    // 内容
    lv_obj_t * content = lv_label_create(screen);
    lv_obj_set_style_text_font(content, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(content, "尊敬的观众您好,为帮助您快速通过自助取票机获取电影票,避免延误观影,敬请阅读以下指引：\n"
                               "1. 请凭取票码或身份证取票\n"
                               "2. 网络购票成功10分钟后可取票\n"
                               "3. 取票机支持微信/支付宝扫码\n"
                               "4. 儿童票需在前台操作\n"
                               "5. 过期未取的票将自动退款\n"
                               "6. 有问题请联系前台工作人员");
    lv_obj_set_pos(content, 50, 80);
    lv_obj_set_width(content, 700);

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(screen);
    lv_obj_set_pos(back_btn, 350, 400);
    lv_obj_set_size(back_btn, 100, 50);
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_obj_set_style_text_font(back_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
    lv_obj_add_event_cb(back_btn, back_to_index_cb, LV_EVENT_CLICKED, NULL);
}

// 座位分布
static void event_cb(lv_event_t * e)
{
    /*The original target of the event. Can be the buttons or the container*/
    lv_obj_t * target = lv_event_get_target(e);

    /*The current target is always the container as the event is added to it*/
    lv_obj_t * cont = lv_event_get_current_target(e);

    /*If container was clicked do nothing*/
    if(target == cont) return;

    /*Make the clicked buttons red*/
    lv_obj_set_style_bg_color(target, lv_palette_main(LV_PALETTE_GREEN), 0);
}

/**
 * Demonstrate event bubbling
 */

// 创建电影票选座布局  6排×9列，含屏幕标识、排数标签
static void create_seat_layout(lv_obj_t * parent)
{
    // 初始化座位数据  6排×9列，模拟部分座位已售
    uint32_t seat_idx = 0;
    for(uint8_t row = 1; row <= 6; row++) {     // 6排
        for(uint8_t col = 1; col <= 9; col++) { // 9列
            seats[seat_idx].row      = row;
            seats[seat_idx].col      = col;
            seats[seat_idx].seat_num = (row - 1) * 9 + col; // 座位号：1~54
            seats[seat_idx].status   = SEAT_AVAILABLE;      // 默认可选

            // 模拟已售座位
            if((row == 1 && col == 1) || (row == 2 && col == 3) || (row == 4 && col == 5) || (row == 5 && col == 2)) {
                seats[seat_idx].status = SEAT_SOLD;
            }
            seat_idx++;
        }
    }

    // 创建座位容器
    lv_obj_t * seat_cont = lv_obj_create(parent);
    lv_obj_set_pos(seat_cont, 80, 100);                              // 座位区域位置  左100，上120
    lv_obj_set_size(seat_cont, 640, 280);                            // 座位区域大小
    lv_obj_set_style_bg_opa(seat_cont, LV_OPA_TRANSP, LV_PART_MAIN); // 透明背景
    // 移除流式布局，改用手动定位
    // lv_obj_set_flex_flow(seat_cont, LV_FLEX_FLOW_ROW_WRAP);          // 自动换行
    lv_obj_set_style_pad_row(seat_cont, 10, LV_PART_MAIN); // 缩小行间距
    // lv_obj_set_flex_align(seat_cont, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 创建屏幕标识
    lv_obj_t * screen_label = lv_label_create(parent);
    lv_obj_set_style_text_font(screen_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(screen_label, "[屏幕]");
    lv_obj_set_style_text_color(screen_label, lv_color_hex(0x666666), LV_PART_MAIN);
    lv_obj_align_to(screen_label, seat_cont, LV_ALIGN_TOP_MID, 0, -20); // 屏幕在座位上方

    // 批量创建座位按钮和排数标签
    for(uint8_t row = 1; row <= 6; row++) {
        // 排数标签 (放在每排的最左边)
        lv_obj_t * row_label = lv_label_create(seat_cont);
        lv_obj_set_style_text_font(row_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
        lv_label_set_text_fmt(row_label, "%d排", row);
        // 正确计算排数标签的位置
        lv_obj_set_pos(row_label, 0, (row - 1) * 45 + 10); // y坐标基于排数计算
        lv_obj_set_size(row_label, 40, 40);
        lv_obj_set_style_text_align(row_label, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);

        // 每排的9个座位
        for(uint8_t col = 1; col <= 9; col++) {
            // 计算当前座位在seats数组中的索引
            uint32_t idx = (row - 1) * 9 + (col - 1);

            lv_obj_t * seat_btn = lv_btn_create(seat_cont);
            lv_obj_set_size(seat_btn, 50, 40);                   // 座位按钮大小
            lv_obj_add_flag(seat_btn, LV_OBJ_FLAG_EVENT_BUBBLE); // 事件冒泡
            seats[idx].btn = seat_btn;

            // 正确计算座位按钮的位置 (在排数标签右侧)
            lv_obj_set_pos(seat_btn, 40 + (col - 1) * 60, (row - 1) * 45 + 5);

            // 根据座位状态设置颜色
            if(seats[idx].status == SEAT_AVAILABLE) {
                lv_obj_set_style_bg_color(seat_btn, lv_color_hex(0xCCCCCC), LV_PART_MAIN); // 可选 灰色
            } else if(seats[idx].status == SEAT_SOLD) {
                lv_obj_set_style_bg_color(seat_btn, lv_color_hex(0xFF4444), LV_PART_MAIN); // 已售 红色
                lv_obj_clear_flag(seat_btn, LV_OBJ_FLAG_CLICKABLE);                        // 已售座位不可点击
            }

            // 座位号标签
            lv_obj_t * num_label = lv_label_create(seat_btn);
            lv_label_set_text_fmt(num_label, "%d", seats[idx].seat_num);
            lv_obj_center(num_label);
        }
    }

    //  绑定座位点击事件
    lv_obj_add_event_cb(seat_cont, seat_click_cb, LV_EVENT_CLICKED, NULL);
}

// 座位点击回调函数
static void seat_click_cb(lv_event_t * e)
{
    lv_obj_t * target = lv_event_get_target(e);         // 被点击的座位按钮
    lv_obj_t * cont   = lv_event_get_current_target(e); // 座位容器

    // 若点击的是容器或排数标签，忽略
    if(target == cont || lv_obj_get_child_cnt(target) == 0) {
        return;
    }

    // 找到被点击的座位索引
    uint32_t seat_idx = 0;
    for(; seat_idx < 54; seat_idx++) {
        if(seats[seat_idx].btn == target) {
            break;
        }
    }
    if(seat_idx >= 54) return; // 未找到座位，异常退出

    // 处理选座/取消选座
    if(seats[seat_idx].status == SEAT_AVAILABLE) {
        // 可选座位  选中 最多选10个
        if(selected_count >= 10) {
            // 超过最大选座数，显示提示
            lv_obj_t * msg = lv_label_create(lv_scr_act());
            lv_obj_set_style_text_font(msg, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
            lv_label_set_text(msg, "最多可选择10个座位!");
            lv_obj_center(msg);
            lv_timer_create(delete_obj_cb, 1500, msg); // 1.5秒后消失
            return;
        }

        seats[seat_idx].status = SEAT_SELECTED;
        lv_obj_set_style_bg_color(target, lv_color_hex(0x4CAF50), LV_PART_MAIN); // 选中  绿色
        selected_seats[selected_count++] = seats[seat_idx].seat_num;             // 加入已选列表
    } else if(seats[seat_idx].status == SEAT_SELECTED) {
        // 已选座位 取消选中
        seats[seat_idx].status = SEAT_AVAILABLE;
        lv_obj_set_style_bg_color(target, lv_color_hex(0xCCCCCC), LV_PART_MAIN); // 取消  灰色

        // 从已选列表中移除
        for(uint8_t i = 0; i < selected_count; i++) {
            if(selected_seats[i] == seats[seat_idx].seat_num) {
                // 前移覆盖被删除的座位号
                for(uint8_t j = i; j < selected_count - 1; j++) {
                    selected_seats[j] = selected_seats[j + 1];
                }
                selected_count--;
                break;
            }
        }
    }

    // 更新已选座位显示
    update_selected_label();
}

/**
 * 更新已选座位显示标签
 */
static void update_selected_label()
{
    if(selected_label == NULL) return;

    if(selected_count == 0) {
        lv_label_set_text(selected_label, "已选座位：无");
        return;
    }

    // 拼接已选座位号
    char buf[64] = "已选座位: ";
    for(uint8_t i = 0; i < selected_count; i++) {
        if(i > 0) strcat(buf, ",");
        char num_str[4];
        sprintf(num_str, "%d", selected_seats[i]);
        strcat(buf, num_str);
    }
    lv_label_set_text(selected_label, buf);
}

// 生成8位取票码：前两位是日期，后六位是时间(HHMMSS)
static void generate_ticket_code(char * code)
{
    time_t now;
    time(&now);
    struct tm * timeinfo = localtime(&now);

    // 格式：DDHHMMSS (日时分秒)
    sprintf(code, "%02d%02d%02d%02d",
            timeinfo->tm_mday, // 日期(01-31)
            timeinfo->tm_hour, // 小时(00-23)
            timeinfo->tm_min,  // 分钟(00-59)
            timeinfo->tm_sec); // 秒(00-59)
}

// 查看取票码按钮回调
static void view_ticket_code_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        show_ticket_code_screen(); // 显示取票码页面
    }
}

// 我的页面
void show_my_page(void)
{
    lv_obj_t * screen = lv_obj_create(NULL);
    lv_obj_set_size(screen, 800, 480);
    lv_scr_load(screen);
    lv_obj_set_style_bg_color(screen, lv_color_hex(0xFAFAFA), LV_PART_MAIN);

    // 标题
    lv_obj_t * title = lv_label_create(screen);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(title, "取票助手");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);

    // 扫码取票区域
    lv_obj_t * scan_area = lv_obj_create(screen);
    lv_obj_set_size(scan_area, 300, 300);
    lv_obj_center(scan_area);
    lv_obj_set_style_border_width(scan_area, 2, LV_PART_MAIN);
    lv_obj_set_style_border_color(scan_area, lv_color_hex(0x00C853), LV_PART_MAIN);

    // 扫码提示
    lv_obj_t * scan_tip = lv_label_create(scan_area);
    lv_obj_set_style_text_font(scan_tip, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(scan_tip, "  该功能尚在开发中~\n\n\n\n\n\n\n\n\n请扫描电影票二维码");
    lv_obj_align(scan_tip, LV_ALIGN_BOTTOM_MID, 0, 0);

    // 查看取票码按钮
    lv_obj_t * view_ticket_btn = lv_btn_create(screen);
    lv_obj_set_pos(view_ticket_btn, 50, 400);
    lv_obj_set_size(view_ticket_btn, 200, 50);
    lv_obj_t * view_ticket_label = lv_label_create(view_ticket_btn);
    lv_obj_set_style_text_font(view_ticket_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(view_ticket_label, "查看取票码");
    lv_obj_center(view_ticket_label);
    lv_obj_add_event_cb(view_ticket_btn, view_ticket_code_cb, LV_EVENT_CLICKED, NULL);

    // 影厅导航按钮
    lv_obj_t * map_btn = lv_btn_create(screen);
    lv_obj_set_size(map_btn, 200, 50);
    lv_obj_align(map_btn, LV_ALIGN_BOTTOM_RIGHT, -50, -30);
    lv_obj_t * map_label = lv_label_create(map_btn);
    lv_obj_set_style_text_font(map_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(map_label, "影厅导航");
    lv_obj_center(map_label);

    lv_obj_add_event_cb(map_btn, map_btn_cb, LV_EVENT_CLICKED, NULL);

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(screen);
    lv_obj_set_size(back_btn, 60, 30);
    lv_obj_align(back_btn, LV_ALIGN_TOP_LEFT, 20, 20);
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_obj_set_style_text_font(back_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
    lv_obj_add_event_cb(back_btn, back_to_index_cb, LV_EVENT_CLICKED, NULL);

    // 5分钟无操作返回首页
    lv_timer_create(inactivity_back_to_index_cb, 300000, NULL);
}

// 在合适的位置添加影厅导航按钮的回调函数
static void map_btn_cb(lv_event_t * e)
{
    // 创建提示信息弹窗
    lv_obj_t * msgbox = lv_obj_create(lv_scr_act());
    lv_obj_set_size(msgbox, 300, 150);
    lv_obj_center(msgbox);
    lv_obj_set_style_radius(msgbox, 10, LV_PART_MAIN);
    lv_obj_set_style_bg_color(msgbox, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    lv_obj_set_style_border_width(msgbox, 2, LV_PART_MAIN);
    lv_obj_set_style_border_color(msgbox, lv_color_hex(0xCCCCCC), LV_PART_MAIN);

    // 添加提示文字
    lv_obj_t * label = lv_label_create(msgbox);
    lv_obj_set_style_text_font(label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(label, "请询问前台工作人员，谢谢配合！");
    lv_obj_center(label);

    // 3秒后自动关闭弹窗
    lv_timer_t * timer = lv_timer_create(delete_obj_cb, 3000, msgbox);
}

// 显示取票码页面
void show_ticket_code_screen(void)
{

    lv_obj_t * screen = lv_obj_create(NULL);
    lv_scr_load(screen);
    lv_obj_set_style_bg_color(screen, lv_color_hex(0xFFFFFF), LV_PART_MAIN);

    // 标题
    lv_obj_t * title = lv_label_create(screen);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(title, "我的取票码");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 30);

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(screen);
    lv_obj_set_pos(back_btn, 20, 20);
    lv_obj_set_size(back_btn, 80, 40);
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_obj_set_style_text_font(back_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
    lv_obj_add_event_cb(back_btn, (lv_event_cb_t)show_my_page, LV_EVENT_CLICKED, NULL);

    // 显示取票码列表
    if(ticket_code_count == 0) {
        lv_obj_t * no_code_label = lv_label_create(screen);
        lv_obj_set_style_text_font(no_code_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
        lv_label_set_text(no_code_label, "暂无取票码");
        lv_obj_center(no_code_label);
    } else {
        // 创建一个列表显示所有取票码
        lv_obj_t * list = lv_list_create(screen);
        lv_obj_set_size(list, 700, 350);
        lv_obj_align(list, LV_ALIGN_CENTER, 0, 0);

        for(int i = 0; i < ticket_code_count; i++) {
            // 格式化创建时间
            char time_str[20];
            struct tm * timeinfo = localtime(&ticket_codes[i].create_time);
            strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", timeinfo);

            // 创建列表项
            char item_text[100];
            sprintf(item_text, "取票码: %s  创建时间: %s", ticket_codes[i].code, time_str);
            lv_obj_t * list_btn = lv_list_add_btn(list, NULL, item_text);
            lv_obj_set_style_text_font(list_btn, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
        }
    }

    // 设置无操作定时器
    if(inactivity_timer) {
        lv_timer_reset(inactivity_timer);
    } else {
        inactivity_timer = lv_timer_create(inactivity_back_to_index_cb, 60000, NULL);
    }
}

// 子页面标识
typedef enum { SETTINGS_HOME, DEVICE_STATUS, NETWORK_SETTINGS, OPERATION_PARAMS } SettingsPage;

static SettingsPage current_page = SETTINGS_HOME;

// 返回上一级页面
static void back_to_prev_cb(lv_event_t * e)
{
    if(current_page != SETTINGS_HOME) {
        // 先保存settings_root指针
        lv_obj_t * temp_root = settings_root;
        // 将settings_root设置为NULL，避免show_settings_main中重复删除
        settings_root = NULL;
        current_page  = SETTINGS_HOME;
        show_settings_main();
        // 安全地删除旧的对象
        if(temp_root) {
            lv_obj_del(temp_root);
        }
    } else {
        back_to_index_cb(e);
    }
}

// 设备状态页面
static void show_device_status_page(void)
{
    current_page  = DEVICE_STATUS;
    settings_root = lv_obj_create(NULL);
    lv_obj_set_size(settings_root, 800, 480);
    lv_obj_set_style_bg_color(settings_root, lv_color_hex(0xFAFAFA), LV_PART_MAIN);
    lv_scr_load(settings_root);

    // 标题
    lv_obj_t * title = lv_label_create(settings_root);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(title, "设备状态监控");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);

    // 状态列表
    lv_obj_t * list = lv_list_create(settings_root);
    lv_obj_set_size(list, 700, 350);
    lv_obj_align(list, LV_ALIGN_BOTTOM_MID, 0, -50);

    // 硬件状态项
    lv_obj_set_style_text_font(list, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_obj_t * printer = lv_list_add_btn(
        list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/Printer.png", "打印机: 正常");
    lv_obj_set_style_text_color(printer, lv_color_hex(0x00C853), LV_PART_MAIN);

    lv_obj_t * scanner = lv_list_add_btn(
        list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/Scanner.png", "扫码器: 正常");
    lv_obj_set_style_text_color(scanner, lv_color_hex(0x00C853), LV_PART_MAIN);

    lv_obj_t * paper = lv_list_add_btn(
        list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/Ticket_ Paper.png", "票纸余量: 75%");
    lv_obj_set_style_text_color(paper, lv_color_hex(0x00C853), LV_PART_MAIN);

    lv_obj_t * network =
        lv_list_add_btn(list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/WIFI.png", "网络: 已连接");
    lv_obj_set_style_text_color(network, lv_color_hex(0x00C853), LV_PART_MAIN);

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(settings_root);
    lv_obj_set_size(back_btn, 80, 35);
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_LEFT, 20, -20);
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_obj_set_style_text_font(back_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
    lv_obj_add_event_cb(back_btn, back_to_prev_cb, LV_EVENT_CLICKED, NULL);
}

// 网络设置页面
static void show_network_page(void)
{
    current_page  = NETWORK_SETTINGS;
    settings_root = lv_obj_create(NULL);
    lv_obj_set_size(settings_root, 800, 480);
    lv_obj_set_style_bg_color(settings_root, lv_color_hex(0xFAFAFA), LV_PART_MAIN);
    lv_scr_load(settings_root);

    // 标题
    lv_obj_t * title = lv_label_create(settings_root);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(title, "网络设置");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);

    // IP地址显示
    lv_obj_t * ip_label = lv_label_create(settings_root);
    lv_obj_set_style_text_font(ip_label, &lv_font_montserrat_16, LV_PART_MAIN);
    lv_label_set_text(ip_label, "IP: 192.168.47.134");
    lv_obj_align(ip_label, LV_ALIGN_TOP_LEFT, 50, 80);

    // 刷新按钮
    lv_obj_t * refresh_btn = lv_btn_create(settings_root);
    lv_obj_set_size(refresh_btn, 120, 40);
    lv_obj_align(refresh_btn, LV_ALIGN_TOP_RIGHT, -50, 80);
    lv_obj_t * refresh_label = lv_label_create(refresh_btn);
    lv_obj_set_style_text_font(refresh_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(refresh_label, "刷新状态");
    lv_obj_center(refresh_label);

    // 可用网络列表
    lv_obj_t * wifi_list = lv_list_create(settings_root);
    lv_obj_set_size(wifi_list, 700, 280);
    lv_obj_align(wifi_list, LV_ALIGN_BOTTOM_MID, 0, -50);
    lv_obj_set_style_text_font(wifi_list, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_list_add_btn(wifi_list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/Movie_Theater.png",
                    "影院专用网络 (信号强)");
    lv_list_add_btn(wifi_list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/Backup_Network.png",
                    "备用网络 (信号中)");

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(settings_root);
    lv_obj_set_size(back_btn, 80, 35);
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_LEFT, 20, -20);
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_obj_set_style_text_font(back_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
    lv_obj_add_event_cb(back_btn, back_to_prev_cb, LV_EVENT_CLICKED, NULL);
}

// 运营参数页面
static void show_operation_page(void)
{
    current_page  = OPERATION_PARAMS;
    settings_root = lv_obj_create(NULL);
    lv_obj_set_size(settings_root, 800, 480);
    lv_obj_set_style_bg_color(settings_root, lv_color_hex(0xFAFAFA), LV_PART_MAIN);
    lv_scr_load(settings_root);

    // 标题
    lv_obj_t * title = lv_label_create(settings_root);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(title, "运营参数");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);

    // 参数列表
    lv_obj_t * list = lv_list_create(settings_root);
    lv_obj_set_size(list, 700, 350);
    lv_obj_align(list, LV_ALIGN_BOTTOM_MID, 0, -50);

    lv_obj_set_style_text_font(list, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_list_add_btn(list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/Power_on.png",
                    "设备开机时间: 08:00");
    lv_list_add_btn(list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/Power_off.png",
                    "设备关机时间: 23:30");
    lv_list_add_btn(list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/Validity_Period.png",
                    "取票码有效期: 24小时");
    lv_list_add_btn(list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/Upper_Limit.png",
                    "单张取票上限: 6张");

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(settings_root);
    lv_obj_set_size(back_btn, 80, 35);
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_LEFT, 20, -20);
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_obj_set_style_text_font(back_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
    lv_obj_add_event_cb(back_btn, back_to_prev_cb, LV_EVENT_CLICKED, NULL);
}

// 设置主页面
void show_settings_main(void)
{
    // 销毁现有页面
    if(settings_root) {
        lv_obj_del(settings_root);
        settings_root = NULL;
    }

    // 创建主页面
    settings_root = lv_obj_create(NULL);
    lv_obj_set_size(settings_root, 800, 480);
    lv_obj_set_style_bg_color(settings_root, lv_color_hex(0xFAFAFA), LV_PART_MAIN);
    lv_scr_load(settings_root);

    // 标题
    lv_obj_t * title = lv_label_create(settings_root);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(title, "设备设置");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);

    // 功能列表
    lv_obj_t * list = lv_list_create(settings_root);
    lv_obj_set_size(list, 700, 380);
    lv_obj_align(list, LV_ALIGN_BOTTOM_MID, 0, -30);

    // 设备状态项
    lv_obj_t * status_btn = lv_list_add_btn(
        list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/Monitor.png", "设备状态监控");
    lv_obj_set_style_text_font(status_btn, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_obj_add_event_cb(status_btn, (lv_event_cb_t)show_device_status_page, LV_EVENT_CLICKED, NULL);

    // 网络设置项
    lv_obj_t * net_btn =
        lv_list_add_btn(list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/Network.png", "网络设置");
    lv_obj_set_style_text_font(net_btn, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_obj_add_event_cb(net_btn, (lv_event_cb_t)show_network_page, LV_EVENT_CLICKED, NULL);

    // 运营参数项
    lv_obj_t * op_btn = lv_list_add_btn(
        list, "S:/nfs/lvgl-arm/lv_port_linux_frame_buffer-release-v8.2/img/Operational.png", "运营参数配置");
    lv_obj_set_style_text_font(op_btn, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_obj_add_event_cb(op_btn, (lv_event_cb_t)show_operation_page, LV_EVENT_CLICKED, NULL);

    // 返回首页按钮
    lv_obj_t * back_btn = lv_btn_create(settings_root);
    lv_obj_set_size(back_btn, 80, 35);
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_LEFT, 20, -20);
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_obj_set_style_text_font(back_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(back_label, "返回首页");
    lv_obj_center(back_label);
    lv_obj_add_event_cb(back_btn, back_to_index_cb, LV_EVENT_CLICKED, NULL);

    // 退出登录按钮 (右下角)
    lv_obj_t * logout_btn = lv_btn_create(settings_root);
    lv_obj_set_size(logout_btn, 100, 35);
    lv_obj_align(logout_btn, LV_ALIGN_BOTTOM_RIGHT, -20, -20);
    lv_obj_set_style_bg_color(logout_btn, lv_color_hex(0xFF4444), LV_PART_MAIN); // 红色背景表示重要操作
    lv_obj_t * logout_label = lv_label_create(logout_btn);
    lv_obj_set_style_text_font(logout_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(logout_label, "退出登录");
    lv_obj_center(logout_label);
    lv_obj_add_event_cb(logout_btn, logout_confirm_cb, LV_EVENT_CLICKED, NULL);
}

// 退出登录确认回调
static void logout_confirm_cb(lv_event_t * e)
{
    // 创建确认对话框
    lv_obj_t * msgbox = lv_obj_create(lv_scr_act());
    lv_obj_set_size(msgbox, 300, 150);
    lv_obj_center(msgbox);
    lv_obj_set_style_radius(msgbox, 10, LV_PART_MAIN);
    lv_obj_set_style_bg_color(msgbox, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    lv_obj_set_style_border_width(msgbox, 2, LV_PART_MAIN);
    lv_obj_set_style_border_color(msgbox, lv_color_hex(0xCCCCCC), LV_PART_MAIN);

    // 标题
    lv_obj_t * title = lv_label_create(msgbox);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(title, "确认退出登录?");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);

    // 内容
    lv_obj_t * content = lv_label_create(msgbox);
    lv_obj_set_style_text_font(content, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(content, "您确定要退出当前账户吗?");
    lv_obj_align(content, LV_ALIGN_CENTER, 0, -10);

    // 确定按钮
    lv_obj_t * yes_btn = lv_btn_create(msgbox);
    lv_obj_set_size(yes_btn, 80, 30);
    lv_obj_align(yes_btn, LV_ALIGN_BOTTOM_LEFT, 30, -20);
    lv_obj_set_style_bg_color(yes_btn, lv_color_hex(0x4CAF50), LV_PART_MAIN);
    lv_obj_t * yes_label = lv_label_create(yes_btn);
    lv_obj_set_style_text_font(yes_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(yes_label, "是");
    lv_obj_center(yes_label);
    lv_obj_add_event_cb(yes_btn, logout_yes_cb, LV_EVENT_CLICKED, msgbox);

    // 取消按钮
    lv_obj_t * no_btn = lv_btn_create(msgbox);
    lv_obj_set_size(no_btn, 80, 30);
    lv_obj_align(no_btn, LV_ALIGN_BOTTOM_RIGHT, -30, -20);
    lv_obj_set_style_bg_color(no_btn, lv_color_hex(0xF44336), LV_PART_MAIN);
    lv_obj_t * no_label = lv_label_create(no_btn);
    lv_obj_set_style_text_font(no_label, &SourceHanSansCN_Light_2_16, LV_PART_MAIN);
    lv_label_set_text(no_label, "否");
    lv_obj_center(no_label);
    lv_obj_add_event_cb(no_btn, logout_no_cb, LV_EVENT_CLICKED, msgbox);
}

// 点击"是"退出登录
static void logout_yes_cb(lv_event_t * e)
{
    lv_obj_t * msgbox = (lv_obj_t *)lv_event_get_user_data(e);
    if(msgbox) {
        lv_obj_del(msgbox); // 删除确认对话框
    }

    // 返回登录页面
    show_second_screen();
}

// 点击"否"取消退出
static void logout_no_cb(lv_event_t * e)
{
    lv_obj_t * msgbox = (lv_obj_t *)lv_event_get_user_data(e);
    if(msgbox) {
        lv_obj_del(msgbox); // 删除确认对话框
    }
}

// 设置页面入口
void show_settings_screen(void)
{
    current_page = SETTINGS_HOME;
    show_settings_main();
}

int main(void)
{
    /*LittlevGL init*/
    lv_init();

    /*Linux frame buffer device init*/
    fbdev_init();

    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];

    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);

    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf = &disp_buf;
    disp_drv.flush_cb = fbdev_flush;
    disp_drv.hor_res  = 800;
    disp_drv.ver_res  = 480;
    lv_disp_drv_register(&disp_drv);

    evdev_init();
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;

    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb      = evdev_read;
    lv_indev_t * mouse_indev = lv_indev_drv_register(&indev_drv_1);

    /*Set a cursor for the mouse*/
    LV_IMG_DECLARE(mouse_cursor_icon)
    lv_obj_t * cursor_obj = lv_img_create(lv_scr_act()); /*Create an image object for the cursor */
    lv_img_set_src(cursor_obj, &mouse_cursor_icon);      /*Set the image source*/
    lv_indev_set_cursor(mouse_indev, cursor_obj);        /*Connect the image  object to the driver*/

    /*Create a Demo*/
    // lv_demo_widgets();
    show_first_img();
    initialize_user_data();
    // show_index_screen();

    /*Handle LitlevGL tasks (tickless mode)*/
    while(1) {
        lv_timer_handler();
        usleep(5000);
    }

    return 0;
}

/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}
